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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

And how can this problem be solved? Plz see: goo.gl/DqEeBs
Is that right? static load<M extends Model>(ModelClass: Type<M> & typeof Model): M {...
I guess so, TypeScript have problem with static methods.
Since 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.
Sure, if you are happy with intersecting with typeof Model, that is a fine solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.