我最近在TypeScript上偶然发现了这种奇怪的行为。在编译期间,只有当期望变量的类型是接口时,如果接口没有强制字段,它才会抱怨过多的属性。链接到TypeScript游乐场#1:
interface IAnimal {
name?: string;
}
class Animal implements IAnimal {
}
var x : IAnimal = { bar: true }; // Object literal may only specify known properties, and 'bar' does not exist in type