microsoft / microsoft/TypeScript
Add getMappedTypeParameters to TypeChecker
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der TypeChecker API und dem im Issue beschriebenen getResolvedSignature-Einstiegspunkt und untersuche anschließend, wie Signature.mapper derzeit inferierte Zuordnungen bereitstellt. Vergleiche das angeforderte Verhalten von getMappedTypeParameters mit dem bestehenden internen Mapper- und Transformer-Workaround. Als abgeschlossen gilt die Aufgabe, wenn TypeChecker inferierte Typparameter für Aufrufausdrücke bereitstellt, ohne interne Signature-Felder zu benötigen, und eine Abdeckung für die hier beschriebenen generischen Aufrufbeispiele vorhanden ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100