Reputation: 15715
I've already seen this other question, but although the name is similar, the question is really a different one, because they're trying to get the type of a property, which happens to be the same as the generic type argument, but I'm trying to get the actual resolved type argument, because I want to get the type T
of Array<T>
, and there's no property of type T
inside the array type.
So I have a function like this:
import ts from 'typescript'
function convertType(tc: ts.TypeChecker, type: ts.Type) {
if (type.symbol.name === 'Array') {
debugger;
// How do I get the resolved type of T?
}
else {
//...
}
}
I've noticed that if I debug the function and hover over type
, it'll have a property called resolvedTypeArguments
which has the exact type I need. So I could just do (type as any).resolvedTypeArguments
, but that'd be a hack and I wonder if there's an official way to do it.
I was guessing I'd have to use one of the is...
functions to cast the type into something that has a resolvedTypeArguments
property, but there's no mention of "resolvedTypeArguments" anywhere in the whole "typescript.d.ts" file, so it seems that I'm not supposed to access that member at all.
Upvotes: 0
Views: 375
Reputation: 9182
You need to get the type of the first type parameter:
const params = checker.getTypeArguments(type);
console.log(checker.typeToString(params[0])); // T
Upvotes: 1