Typescript decorate class add new method

user7122183

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();
  }
}
martin

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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Automatically decorate every method in a class

From Dev

Add new method to existing class

From Dev

Create method within method in a new Class in typescript

From Dev

TypeScript class decorators - add class method

From Dev

TypeScript - How to add a method outside the class definition

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Add new member to class used in override method

From Dev

How would one decorate an inherited method in the child class?

From Dev

How do I decorate an instance method with another class in Python 2.7?

From Dev

How can I decorate an instance method with a decorator class?

From Dev

How do I add a static method to my TypeScript class?

From Dev

How do I add a static method to my TypeScript class?

From Dev

typescript decorate setter function

From Dev

Is it possible to decorate a decorator in Typescript?

From Dev

How to use trait to add new method to class in Scala?

From Dev

decorate method after declaration

From Dev

How to seamlessly decorate class

From Dev

decorate specific methods of a class

From Dev

Typescript overridden class method and this

From Dev

can remote config of Firebase handle when I add a new class or a new method?

From Dev

Typescript: Decorate constructor public param?

From Dev

Ruby add method to a class

From Dev

Add @property method to a class

From Dev

Method __add__ in a class

From Dev

Add method to a class

From Dev

Java add method to class

From Dev

In python 2.7, how can I wrap a class instance method or decorate it with a try/except block?

From Dev

How to New Up a Class in TypeScript

From Dev

Typescript create new instance in class

Related Related

HotTag

Archive