type Foo = {
fn1(field: string | undefined): void
}
class Test implements Foo {
fn1(field: string): void {
console.log(field.toLowerCase())
}
}
const x: Foo = new Test()
void x.fn1(undefined) // Compiles but crashes at runtime
Expected Behavior: I expected TypeScript to give a compile-time error, such as: "Class 'Test' incorrectly implements interface 'Foo'" since the method's signature does not match the interface's.
In particular, the fn1 method in the class Test accepts only string, but the interface Foo allows string | undefined. This mismatch leads to a runtime crash when undefined is passed to fn1.
Problem: It compiles successfully without any error, but crashes at runtime. Why does TypeScript not flag this as an error at compile-time, and how can I catch such issues?