My code:
class Model {}
class Loader {
static load<M extends Model>(ModelClass: typeof Model): M {
return new ModelClass();
}
}
Error message: Type 'Model' is not assignable to type 'M'.
Link to Playground: goo.gl/qrSsoX
We should say that load take a class as argument not an instance.
export interface Type<T> extends Function { new (...args: any[]): T; }
class Model {}
class Loader {
static load<M extends Model>(ModelClass: Type<M>): M {
return new ModelClass();
}
}
for more information take a look at this answer
static load<M extends Model>(ModelClass: Type<M> & typeof Model): M {...Type<T> is an interface for the static side of the class (i.e., the constructor function), not the instance, you can declare m1 directly as a method of Type<T> without the static modifier.typeof Model, that is a fine solution.