摘自《 Basarat-深入研究TypeScript》
interface Point2D {
x: number;
y: number;
}
interface Point3D {
x: number;
y: number;
z: number;
}
var point2D: Point2D = { x: 0, y: 10 }
var point3D: Point3D = { x: 0, y: 10, z: 20 }
function iTakePoint2D(point: Point2D) { /* do something */ }
iTakePoint2D(point2D); // exact match okay
iTakePoint2D(point3D); // extra information okay
iTakePoint2D({ x: 0 }); // Error: missing information `y`
我替换此行:
iTakePoint2D(point3D); // extra information okay
使用
iTakePoint2D({ x: 0, y: 10, z: 20 });
,我有一个错误。为什么现在多余的信息行不通了?
答案 0 :(得分:0)
对象文字在分配给其他类型或作为参数传递给函数时,总是经过多余的属性检查。
https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks