Consider the following:
type X = {
a: number;
};
type Y = X & { b: number };
abstract class XX {
abstract yy(x: X): void;
}
class YY implements XX {
yy(x: Y): void {
console.log(x.b);
}
}
const xxs: XX[] = [new YY()];
xxs[0].yy({ a: 1 });
The code above compiles without errors, but why does TS allow a concrete class (YY) to implement an abstract method (yy) receiving a parameter of a narrower type (Y) of the type declared in the abstract class (X)?
This makes the method yy
accept a X
input without compiler errors though the signature and contents of the method clearly expects an Y
.