I have the following type and function signature
type Ctor<T> = {
new (): T
}
export function foo<T>(Ctor: Ctor<T>) {
}
class Bar { foo = 'what' }
foo(Bar) // inferred type of T is Bar
I trying to get the inferred type of T at the call site foo(Bar) using the compiler api. So far I've tried,
if (ts.isCallExpression(node)) {
const funcType = typeChecker.getTypeAtLocation(node.expression)
}
But this only gets the declared type of foo. It doesn't have the type arguments passed to it at the call site. I've also tried,
if (ts.isCallExpression(node)) {
const args = node.typeArguments
}
But this is also empty, I think because the types are not explicitly passed.
So how do I get the inferred type of T at each call site?
Tis the type of the argument you're passing to the function, so you already know it. Can you show a full minimal example of what you've tried, and what you'd like to get?typeof Barbut T is typeBar. So I cant directly getTby checking the type of the argument.foocall (which should befoo<Bar>(Bar)) through the compiler API. The two attempts they have shown aren't successful.