microsoft / microsoft/TypeScript

Add getMappedTypeParameters to TypeChecker

Ouverte
#38,884 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

In Discussion Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’API TypeChecker et le point d’entrée getResolvedSignature décrit dans l’issue, puis examinez comment Signature.mapper fournit actuellement les mappages inférés. Comparez le comportement demandé de getMappedTypeParameters avec le workaround existant utilisant le mapper et le transformer internes. La tâche est terminée lorsque TypeChecker expose les paramètres de type inférés pour les expressions d’appel sans nécessiter les champs internes de Signature, avec une couverture des exemples d’appels génériques décrits ici.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
30/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.