javascript - Typescript 装饰类添加新方法
我想向一个装饰过的类添加一个新方法。一切都正常工作,但编译器抱怨该方法不存在,我该如何让编译器满意?
export function decorate( constructor : Function ) {
constructor.prototype.someMethod = function () {
}
}
@decorate
class Test {
constructor() {
//Property 'someMethod' does not exist on type 'Test'.
this.someMethod();
}
}
答案#1
你可以使用:
(<any>this).someMethod();
或:
this['someMethod']();
你不能使用接口来检查 this 是否包含方法 someMethod(),因为你实际上并没有实现该接口,所以我认为这两种是唯一的选择...
