mercredi 8 juillet 2020

is there any way to bypass ts 2339 in to access class methods OR is it possible to define class methods outside of class in typescript?

What I intend to do is as follows

Class A {
  constructor() {
    bind(this); 
  }
  hello(){
    this.method1();  // <-- I will get error at this line saying method one does not exist on typeOf A
  }
}
function bind(thisReference) {
  function method1() {
    console.log('in method 1');
  }
  thisReference.method1 = method1.bind(thisReference)
}
var a = new A();
a.hello();

Please note there are other cases of object where we get ts2339, which can be solved either by defining types or by using any

I am particularly looking to solve this case and therefore separate question. It is very useful when defining class components in react, so that we can easily define new methods on class outside of class.

Aucun commentaire:

Enregistrer un commentaire