【发布时间】:2016-03-09 10:09:54
【问题描述】:
在 TypeScript 中,类型声明可以允许动态对象属性吗?
class Animal {
name: string;
[everything else]: any;
}
let animal = <Animal>{ name: "Kitty", type: "cat" };
animal.name; // Would be treated as string
animal.type; // Would allow compilation and be treated as any
我希望动态允许这些额外的属性,而不必将它们添加到类型声明中。使用 TypeScript 1.8.2。
【问题讨论】: