Instantiating child class from a static method in base class, using TypeScript

Tristan

Being new to TypeScript, what is the best method to implement a static factory in a base class that instantiates the child class type. For instance, consider a findAll method in a base model class:

class BaseModel {
  static data: {}[];
  static findAll() {
    return this.data.map((x) => new this(x));
  }
  constructor(readonly attributes) {
  }
}

class Model extends BaseModel {
  static data = [{id: 1}, {id: 2}];
  constructor(attributes) {
    super(attributes);
  }
}

const a = Model.findAll();  // This is BaseModel[] not Model[]

This returns BaseModel[] rather than Model[].

Tristan

To answer my own question, this turns out to be a well known issue in TypeScript. The Github issue Polymorphic this for static methods has a long discussion. The solution is as follows:

export type StaticThis<T> = { new (): T };

export class Base {
    static create<T extends Base>(this: StaticThis<T>) {
        const that = new this();
        return that;
    }
    baseMethod() { }
}

export class Derived extends Base {
    derivedMethod() { }
}

// works
Base.create().baseMethod();
Derived.create().baseMethod();
// works too
Derived.create().derivedMethod();
// does not work (normal)
Base.create().derivedMethod();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call method of class from another package without instantiating the class

From Java

Java : call child class method from parent class using interface

From Java

Redefining static method in child class

From

How to call a static method from a private base class?

From Dev

calling child class method from base class C#

From Dev

Return a new instance of Child class from base class static method

From Dev

Calling base class method after child class __init__ from base class __init__?

From Dev

How to create a child class in TypeScript using parent static method?

From Dev

What are the best way to enhance base class method from child class

From Dev

Typescript: Override static factory method of parent Class in Child method

From Dev

Child Method not recognised from Parent class TypeScript

From Dev

Typescript: Access static members of a class without instantiating it from an array of Constructor signatures

From Dev

Typescript: Work with Child Class in static method

From Dev

TypeScript: access static method of a class

From Dev

Instantiating static object of a Class

From Dev

Using static void Main() method from base class as a program's entry point

From Dev

Base class static method using constants of derived class

From Dev

Why does calling a method from base class calls the child method?

From Dev

Calling an extention method of a base class from child class

From Dev

Instantiating class using tuples from a list in Python

From Dev

Access child class static member from a base class static member

From Dev

Using toString() method from child class

From Dev

Is it possible to prevent a child method from running by using 'return' in the base class method when overriding?

From Dev

Typescript Set Value of Class itself in Method, Inheritance from Base Class

From Dev

How to return subclass objects from its base class static method?

From Dev

How to access base class method *args from child class object?

From Dev

Python: check if method is static from class without instantiating

From Dev

getting child class from static method of parent class in Java

From Dev

How to call parent static method using child class in kotlin?

Related Related

  1. 1

    Call method of class from another package without instantiating the class

  2. 2

    Java : call child class method from parent class using interface

  3. 3

    Redefining static method in child class

  4. 4

    How to call a static method from a private base class?

  5. 5

    calling child class method from base class C#

  6. 6

    Return a new instance of Child class from base class static method

  7. 7

    Calling base class method after child class __init__ from base class __init__?

  8. 8

    How to create a child class in TypeScript using parent static method?

  9. 9

    What are the best way to enhance base class method from child class

  10. 10

    Typescript: Override static factory method of parent Class in Child method

  11. 11

    Child Method not recognised from Parent class TypeScript

  12. 12

    Typescript: Access static members of a class without instantiating it from an array of Constructor signatures

  13. 13

    Typescript: Work with Child Class in static method

  14. 14

    TypeScript: access static method of a class

  15. 15

    Instantiating static object of a Class

  16. 16

    Using static void Main() method from base class as a program's entry point

  17. 17

    Base class static method using constants of derived class

  18. 18

    Why does calling a method from base class calls the child method?

  19. 19

    Calling an extention method of a base class from child class

  20. 20

    Instantiating class using tuples from a list in Python

  21. 21

    Access child class static member from a base class static member

  22. 22

    Using toString() method from child class

  23. 23

    Is it possible to prevent a child method from running by using 'return' in the base class method when overriding?

  24. 24

    Typescript Set Value of Class itself in Method, Inheritance from Base Class

  25. 25

    How to return subclass objects from its base class static method?

  26. 26

    How to access base class method *args from child class object?

  27. 27

    Python: check if method is static from class without instantiating

  28. 28

    getting child class from static method of parent class in Java

  29. 29

    How to call parent static method using child class in kotlin?

HotTag

Archive