【发布时间】:2023-03-29 13:40:02
【问题描述】:
我有一个实现接口的对象:
interface ILoadedComponent {
property1:string;
property2: numberl;
}
class SomeClass {
loadedComponent:ILoadedComponent = {
property1:'x',
property2: 10
}
}
但我想在已实现的接口上增加额外的属性:
class SomeClass {
loadedComponent:ILoadedComponent = {
property1: 'x',
property2: 10,
property3: 'y',
property4: 60
}
}
为了扩展已实现的接口,我得到一个编译器错误:
... is not assignable to type SomeClass ...
Object literal may only specify known properties, and 'property3' does not exist in type 'SomeClass '.
如何使用对象字面量扩展接口
【问题讨论】:
-
这是因为对对象文字进行了过多的属性检查。你可以用
as ILoadedComponenttypescriptlang.org/play/index.html#code/…
标签: typescript