Can I get a type name of a generic type?

I have the signature of the execute<TResult>(): Observable<TResult> method execute<TResult>(): Observable<TResult>

How do I get a name like TResult?

Example:

execute<ViewModel> → "ViewModel" is the result I need.

+7
generics reflection typescript typename
source share
1 answer

As far as I know, it is impossible to get the name TResult , but if you provide the appropriate design function, you can get the name.

Declaration:

 execute<TResult>(ctor: { new (): TResult }) : <TResult> { console.log(ctor.name) //Prints out SomeClass return <any>null; } 

Application:

 execute<SomeClass>(SomeClass); 
+8
source share

All Articles