Being new to TypeScript, what's the best way to implement a static factory in a base class that instantiates a type of a child class. For example, consider a method findAllin the base class of a model:
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 returns BaseModel[], not Model[].
source
share