Search code examples
genericstypescriptinstances

How to refer to the T (Generic type) of the class


I have a class of a generic type as follows:

SomeGenericClass<T>{
    constructor(){

    }
}

within some of its' functions i log some messages and would like to refer to the current type T of the generic class in my log.

I've tried

  • T.constructor.name
  • Object.getPrototypeOf(T).constructor.name

with no success, both produce cannot find name 'T'

Any ideas how can i achieve this?

Thanks in advance!


Solution

  • Since TypeScript is always being compiled into Javascript, there is no runtime information about the generic type since there are no generics in Javascript.
    If you need runtime information you need to pass this information as a constructor parameter.