Skip to content

Add typeChecker API to get the inferred type arguments of call expressionΒ #59637

Closed
@zcbenz

Description

@zcbenz

πŸ” Search Terms

infer type arguments function call expression

βœ… Viability Checklist

⭐ 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<T>(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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions