I have two classes: Model and User. User extends the Model.
export Model {
id: number;
static fromData<T>(data: any): T {
return Object.assign(new Model(), data);
}
}
export User extends Model {
name: string;
sayHi(): string {
return 'Hi, ' + this.name;
}
}
The way I wanted to use it looks like this:
const currentUser = User.fromData(dataFromServer);
const message = currentUser.sayHi();
Method hi() doesn't work because I have created an instance of Model class.
How to use TypeScript generics to get an instance of derived class using base class static method?
I'm planning number of different entities with common.
I saw this answer but I don't know how to pass parameters into a static method in my case.
this: new () => T
. Is there a documentation on this feature somewhere? – Aliment