0

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

Khusamov Sukhrob
  • 681
  • 1
  • 8
  • 22

1 Answers1

1

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

Buggy
  • 3,539
  • 1
  • 21
  • 38