Add typeChecker API to get the inferred type arguments of call expression (original) (raw)
🔍 Search Terms
infer type arguments function call expression
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
interface TypeChecker { getTypeArgumentsOfSignature(signature: Signature): readonly Type[]; }
📃 Motivating Example
For the below code, the proposed API is supposed to return [ "text" ] for the Passthrough("text") node.
function Passthrough(value: T) { return value; }
let str = Passthrough("text");
💻 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: