I don't think is possible but it's worth asking. In a generic method or class, I would like to log the name of the generic T type that has been used, I need it as string.
Imagine a method like this:
static getTypeName<T>(): string {
return typeof T; // This is wrong
}
I would expect to be able to use it this way:
let m = getTypeName<Message>(); // Expected string "Message"
let p = getTypeName<Person>(); // Expected string "Person"
The above getTypeName is wrong, it won't even build. typeof would be transpiled in JS and wouldn't produce the desired effect. I tried various things but with no success.
Is there any way to actually do it?
EDIT: Why this is not a duplicate of: this question
The question and the example code shows how to get back an object and not the name of the type specified. Also the method is not generic because if you try to pass in a Type that has constructor parameters it will show the error: "[ts] Argument of type 'typeof MyType' is not assignable to parameter of type 'new () => MyType'." Moreover, if you use the function within a generic method you cannot use T as parameter, this means that everywhere I would need to get the name from the generic Type, I would need to add the ctor to the parameters.