Shreelakshmi G
Shreelakshmi G

Reputation: 119

How to apply Generic type for a variable in typescript

For functions, or for return type we will use generic type <T>, it is not throwing any error, but when i was trying to define a variable with generic type, it is throwing error, i can't able to define, here is the code snippet.

export class SqHandlerTemplate {
    component: ComponentType<T>;
    config?: SqSnackBarConfig;
    url?: string;
} 

For the component variable, i am trying to give <T>, it is throwing error as cannot find name T or is there any restriction that we need to use T only for functions or return type?, please clarify me and help me out in resolving the error. Thanks in advance.

Upvotes: 0

Views: 76

Answers (1)

Qortex
Qortex

Reputation: 7466

You just need to put the template argument in the class name. You forgot <T> after SqHandlerTemplate.

So your code should be:

export class SqHandlerTemplate<T> {
    component: ComponentType<T>;
    config: SqSnackBarConfig;
    url: string;
}

You have more information in the Typescript docs.

Upvotes: 2

Related Questions