The following code is clearly incorrectly typed, yet I cannot make TypeScript error on it. I turned on strict, as well as strictNullChecks and strictFunctionTypes for good measure, but TS remains unwavering in its conviction that this code is fine and dandy.
abstract class A {
// You can pass an undefined to any A
public abstract foo(_x: number | undefined);
}
class B extends A {
// B is an A, but it prohibits passing in an undefined.
// Note that if we did `x: string`, TS would flag it as
// an error.
public foo(x: number) {
if (x === undefined) {
throw new Error("Type error!");
}
}
}
function makeAnA(): A {
// This typechecks correct, so B is clearly an A, in
// TS's opinion.
return new B();
}
function test() {
const b = makeAnA();
// b is a B, so this should not be possible
b.foo(undefined);
}
Is this expected behavior? Is there some option I can turn on that will flag this as an error? I've had this bite me more than once.