【发布时间】:2018-05-08 09:33:56
【问题描述】:
这是 TypeScript 文档中有关装饰器的一些代码:
function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {
return class extends constructor {
newProperty = "new property";
hello = "override";
}
}
@classDecorator
class Greeter {
property = "property";
hello: string;
constructor(m: string) {
this.hello = m;
}
}
console.log(new Greeter("world"));
但是,如果您尝试使用 newProperty,则会收到转译器错误:
类型“Greeter”上不存在属性“newProperty”。
你如何输入这个让编译器知道newProperty实际上存在?
【问题讨论】:
标签: typescript typescript-decorator