Repository navigation
Add typeChecker API to get the inferred type arguments of call expression #59637
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Aug 15, 2024 We don't have a function for this internally (weird, I know - we always operate over and compose
TypeMapperobjects, not concrete type arguments), but I think internally to the checker, the implementation is justfunction getTypeArgumentsForResolvedSignature(signature: Signature) { return instantiateTypes((signature.target || signature).typeParameters, signature.mapper); }
(so long as you're OK with an unresolved signature returning its' input type parameter list). You can't implement it publicly because
.target,.mapper, andinstantiateTypesare all internal, but it seems like a reasonable thing to want to know about a resolved signature.RyanCavanaugh commented
on Aug 16, 2024 MemberMore actionsApproved to expose the above function
- addedHelp WantedYou can do thisYou can do thisand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Aug 16, 2024 I have the following use case of the type checker public API. Is that impossible since
You can't implement it publicly because .target, .mapper, and instantiateTypes are all internal
- Get the global
Iterabletype - Instantiate it with any:
Iterable<any, any, any> - Compare it with another type via
checker.isTypeAssignableTo
- Get the global
I have the following use case of the type checker public API. Is that impossible since
You can't implement it publicly because .target, .mapper, and instantiateTypes are all internal
- Get the global
Iterabletype - Instantiate it with any:
Iterable<any, any, any> - Compare it with another type via
checker.isTypeAssignableTo
TL; DR:
How can I use TypeScript's type checker API to determine if a
ts.Typeis iterable (a.k.a. assignable toIterable<any, any, any>).- Get the global
- added a commit that references this issue
on Oct 11, 2024 - added a commit that references this issue
on Jan 21, 2025 - added a commit that references this issue
on Jan 24, 2025
🔍 Search Terms
infer type arguments function call expression
✅ Viability Checklist
⭐ Suggestion
📃 Motivating Example
For the below code, the proposed API is supposed to return
[ "text" ]for thePassthrough("text")node.💻 Use Cases
In my own case, I'm writing a TypeScript to C++ compiler and getting the inferred type argument is essential for translating the generic function call to C++.
There are also a few other open source projects getting the information by hacking TypeScript's internals:
https://github.com/GoogleFeud/ts-macros/blob/e65480a7bc54107e935e9a95ecbb4f3825f2d6e2/src/utils.ts#L259-L270
https://github.com/mxsdev/ts-type-explorer/blob/3b0bb21f574ebb0d70aac84c37e81e1df0b1d9e4/packages/api/src/util.ts#L725-L750
https://github.com/johnW-ret/express-openapi-gen/blob/885e498066f51fccaa00615f0b46b070788477c9/src/index.ts#L45C18-L84