Inheriting static methods in typescript

I am generating TypeScript classes that use Inheritance. Each of the classes uses a static create function, which may be different.

class A {
  static create(arg: string): A {
    // use args to create A
    return new A()
  };
} 

class B extends A {
  static create(argsForB: number): B {
    // use argsForB to create B
    return new B()
  };
}

Since static functions should not be inherited, this error makes no sense:

Class static side 'typeof B' incorrectly extends base class static side 'typeof A'.
  Types of property 'create' are incompatible.
    Type '(argsForB: number) => B' is not assignable to type '(arg: string) => A'.
      Types of parameters 'argsForB' and 'arg' are incompatible.
        Type 'string' is not assignable to type 'number'.

I know that one solution would be to simply rename create-funtion to createA and createB, but that doesn't seem right.

+4
source share

Source: https://habr.com/ru/post/1687072/


All Articles