【发布时间】:2020-08-20 01:41:24
【问题描述】:
如何在不丢失类名和静态属性和方法的情况下使用装饰器正确扩展类构造函数。
阅读手册有一个注释说
https://www.typescriptlang.org/docs/handbook/decorators.html#class-decorators
NOTE Should you choose to return a new constructor function,
you must take care to maintain the original prototype.
The logic that applies decorators at runtime will not do this for you.
如果我这样做 - 就像在手册中那样 - 我会丢失类名和静态方法
function my_decorator<T extends { new(...constr_args:any[]):any }>(constr_func:T){
return class extends constr_func {
constructor(...args: any[]){
// DO STUFF
super(...args);
// DO STUFF
}
}
}
【问题讨论】:
标签: typescript constructor typescript-decorator