Closed
Description
Consider this example:
class T1 {
prop1: string = 'T1/prop1';
}
class T2 extends T1 {
prop2: string = 'T2/prop2';
}
class A {
propA: string;
checkValue(value: T1) {
console.assert(!!value.prop1);
}
}
class B extends A {
propB: string;
checkValue(value: T2) {
console.assert(!!value.prop2);
}
}
B::checkValue(T2)
can override A::checkValue(T1)
because T2
is assignable to T1
(as it is its subclass).
However, following code, while perfectly valid, will break the second assertion:
const a: A = new B();
const t1: T1 = new T1();
a.checkValue(t1); // B.prototype.checkValue(), expecting T2 but getting T1.