RedJohn RedJohn - 27 days ago 18
TypeScript Question

Typescript decorate class add new method

I want to add a new method to a decorated class. Everything works well but the compiler complains that the method does not exist, how can I satisfy the compiler?

export function decorate( constructor : Function ) {

constructor.prototype.someMethod = function () {

}

}

@decorate
class Test {
constructor() {
//Property 'someMethod' does not exist on type 'Test'.
this.someMethod();
}
}

Answer

You could use:

(<any>this).someMethod();

or:

this['someMethod']();

You can't use interfaces to check that this contains method someMethod() because you're not in fact implementing the interface so I think these two are the only options...

Comments