microsoft / microsoft/TypeScript
Add getMappedTypeParameters to TypeChecker
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
TypeMapper, mapper, mapper1, mapper2
Suggestion
To help transformer developers: Add a helper on TypeChecker to provide (and expose) the inferred (mapped) types of a source function call. i.e.
given the source:
function foo<C>(ctor: C)...
foo(Bar);
provide transformer developers a method (on ts.TypeChecker)
getMappedTypeParameters(node: ts.CallExpression): ts.Type[]
so they can get the inferred TypeParameter nodes for a given source call-expression. By adding a helper method, it does not expose the internal mapping, limits the required maintenance if the internal mapping is ever changed, yet allows developers access to the inference information.
Use Cases
During AST code transform, the compiler has already inferred the types passed to source functions, without them being explicitly defined as a type parameter in the call. The current Signature (typeChecker.getResolvedSignature(node)) does not expose these inferred types and the ```signature.typeParameters`` remains undefined
For transformers that require this information, the current work around is use the internal "mapper" field on Signature. This mapper field changed in ts 3.9 and potentially could change again. Exposing an ability to access this mapping via the TypeChecker should help reduce breakage and limit maintainance required by the TypeScript Team to expose and execute the mapping.
Examples
In my DI transformer, I attempt to infer Type parameters and use them to manipulate the compiled output to configure the DI Container. For example; the source code:
function main() {
let kernel = new Kernel();
kernel.bind(toSelf(Bar));
kernel.bind(toSelf(Foo));
}```
is transpiled to:
```js
function main() {
let kernel = new pigly_1.Kernel();
kernel.bind(Symbol.for("Bar"), pigly_1.toSelf(Bar));
kernel.bind(Symbol.for("Foo"), pigly_1.toSelf(Foo, ctx => ctx.resolve({ service: Symbol.for("Bar"), name: "bar" }).first()));
}
As relevant to this proposal, my transpiler needs to be able to infer the TypeParameter of .bind<T> so that it can emit a symbol for it into the runtime code.
My Current work around to discover the inferred mappings is here:
https://github.com/pigly-di/pigly/blob/develop/packages/pigly-transformer/src/index.ts#L284-L331
Admittedly this is a very niche proposal (if allowed I'd happily do a PR to add it and do the tests) - but at least one other developer had trouble with this: https://stackoverflow.com/questions/48886508/typechecker-api-how-do-i-find-inferred-type-arguments-to-a-function
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeChecker API and the getResolvedSignature entry point described in the issue, then inspect how Signature.mapper currently provides inferred mappings. Compare the requested getMappedTypeParameters behavior with the existing internal mapper and transformer workaround. Done means the TypeChecker exposes inferred type parameters for call expressions without requiring internal Signature fields, with coverage for the generic-call examples described here.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100