When examining the given code, it raises a question about why the TypeScript compiler permits the assignment const c1: I = new C();
. The issue arises when the function call c1.z(args);
results in an error due to the absence of the first
property in the args
object. Shouldn't the compiler identify that an instance of the C
class is incompatible with the I
interface since the z
function in C
requires an argument with the first
property?
interface CArgs {
first: number;
second: number;
}
interface IArgs {
second: number;
}
class C {
z(args: CArgs) {
args.first.toString();
}
}
interface I {
z(args: IArgs): void;
}
const args = {
second: 2
};
const c1: I = new C();
c1.z(args);