I have a specific type and function signature that I'm working with:
type Constructor<T> = {
new (): T
}
export function bar<T>(Constructor: Constructor<T>) {
}
class Foo { bar = 'example' }
bar(Foo) // the inferred type of T here should be Foo
I am attempting to obtain the inferred type of T at the point where bar(Foo)
is called by utilizing the compiler API. My initial attempts include:
if (ts.isCallExpression(node)) {
const funcType = typeChecker.getTypeAtLocation(node.expression)
}
This, however, only retrieves the declared type of bar
, but does not provide the type arguments passed to it at the call location. Another approach I tried is:
if (ts.isCallExpression(node)) {
const args = node.typeArguments
}
Unfortunately, this method turns out to be ineffective as well since the types are not explicitly specified.
So, my question remains: How can I retrieve the inferred type of T at each call site?