【发布时间】:2019-10-06 06:47:20
【问题描述】:
所以,我试图建立对 Typescript 装饰器的理解,但我一直停留在给出的关于类装饰器的示例上。给出的示例展示了如何通过 function(){} 形成类装饰器。
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"));
什么是:
return class extends constructor {
newProperty = "new property";
hello = "override";
}
函数如何返回扩展参数的“类”关键字(称为“构造”)?我很困惑。
这里是原始源的链接(只需滚动到类装饰器的中间部分):https://www.typescriptlang.org/docs/handbook/decorators.html
感谢任何帮助!
【问题讨论】:
标签: typescript typescript-decorator class-decorator