0

Consider this class (it is supposed to be immutable):

class A {

  normalMethod1(): A{
    const instance = A.staticMethod1();
    return instance;
  }

  static staticMethod1: A(){
    return new this();
  }

}

it works fine, but how can I replace A.staticMethod with the current class name? Something like this.staticMethod (which doesn't work because i get ts2576 error) And also same thing for the return types, how to replace them with current class?

The reason for this is that if I ever change the class name, I would have to do it in one place instead of everywhere

Alex
  • 66,732
  • 177
  • 439
  • 641

1 Answers1

0
class A {

  normalMethod1(): A{
    const instance = this.constructor.staticMethod1();
    return instance;
  }

  static staticMethod1: A(){
    return new this();
  }

}

Further reading https://stackoverflow.com/a/28648214/5089567

Konrad
  • 21,590
  • 4
  • 28
  • 64