14

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.

Community
  • 1
  • 1
Rookian
  • 19,841
  • 28
  • 110
  • 180

1 Answers1

13

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);
Rookian
  • 19,841
  • 28
  • 110
  • 180
  • Thanks! I'm not quite sure what happens there. Can you please elaborate on the new(): TResult part? My first guess is that this creates an anonymous constructor-class splatting its properties into a new object that we access via the ctor-parameter. – Michael Kargl Dec 29 '18 at 21:10