I am reading Typescript handbook, in the chapter of interface, I found a question:
interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
The printLabel
need an object with a label:string
property, but the object we passed has another property called size
. It is OK cause the compiler only checks that at least the ones required are present and match the types required.
But, we I call printLabel
in this way:
printLabel({size: 10, label: "Size 10 Object"});
The compile throws an exception.
so why?