Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 867
Kesem David Avatar asked Dec 02 '25 06:12

Kesem David


1 Answers

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.

like image 117
Sebastian Avatar answered Dec 03 '25 23:12

Sebastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!