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

前端 未结 1 863
天命终不由人
天命终不由人 2021-01-04 15:56

I have a method signature of execute(): Observable

How do I get the name of the TResult type?

Example:

相关标签:
1条回答
  • 2021-01-04 16:15

    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);
    
    0 讨论(0)
提交回复
热议问题