microsoft / microsoft/TypeScript
Add getMappedTypeParameters to TypeChecker
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la TypeChecker API e l’entry point getResolvedSignature descritto nell’issue, quindi esamina come Signature.mapper fornisce attualmente le mappature inferite. Confronta il comportamento richiesto di getMappedTypeParameters con il workaround esistente basato su mapper e transformer interni. Il lavoro è completo quando TypeChecker espone i parametri di tipo inferiti per le espressioni di chiamata senza richiedere i campi interni di Signature, con una copertura per gli esempi di chiamate generiche descritti qui.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100