0

I have a predefined interface:

export declare interface Type<T> extends Function {
    new (...args: any[]): T;
}

I would like to have a class attribute that constraint the generic type. Something like this:

public componentType: Type<any extends MyComponentType>;

Is it possible ? Because I did not find the solution on Typescript doc and I can't find a syntax that is working.

Thanks.

2
  • 1
    Maybe stackoverflow.com/questions/26652179/… can give you an answer Commented Feb 18, 2020 at 16:03
  • 2
    why not just public componentType: Type<MyComponentType>? Commented Feb 18, 2020 at 16:25

2 Answers 2

0

Does this do what you want

export declare interface Type<A,T extends A = A> extends Function {
    new (...args: any[]): T;
}

class MyComponentType {

}

class MyComponentType2 extends MyComponentType{

}

class foo {
    public componentType: Type<MyComponentType>;

    constructor() {
        this.componentType = MyComponentType2;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The answer is the comment made by @Josh Wulf on the original post.

public componentType: Type<MyComponentType>

Comments

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.