Suppose I have a parent class with a static method that returns an instance,
abstract class AbstractManager {
private static instance: AbstractManager;
constructor() {
AbstractManager.instance = this;
}
public static getInstance(): AbstractManager {
return this.instance
}
}
And I have a child class which extends the parent class,
class Manager extends AbstractManager {
constructor() {
super();
}
}
If I invoke Manager.getInstance() the return type is of the parent AbstractManager.
Is there a way to set up the parent getInstance() method so that it returns the child type instead?
Obviously, I can add a getInstance() to each of the children class returning or casting to the child type but should I have 100 subclasses then I'll need to write the method 100 times.
Although my knowledge of generics is limited, another solution I have attempted using generics is,
public static getInstance<T extends AbstractManager>(): T {
return this.instance;
}
But this has caused the error Type 'AbstractManager' is not assignable to type 'T'.
Is it possible to set it up such that
Manager.getInstance() // is of type Manager not AbstractManager.
Or should I approach this problem in a different way? Thank you.
