Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the type name of a generic type?

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

How do I get the name of the TResult type?

Example:

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

like image 903
Rookian Avatar asked Dec 15 '16 09:12

Rookian


1 Answers

As far as I know it is not possible to get the name of TResult, but if you provide the accordingly constructor function you can get the name.

Declaration:

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

Usage:

execute<SomeClass>(SomeClass);
like image 183
Rookian Avatar answered Oct 17 '22 05:10

Rookian