microsoft / microsoft/TypeScript

Refactoring: Generate type annotation from inferred type

Ouverte
#32,319 5 commentaires 16 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Domain: LS: Refactorings Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Search Terms

refactoring
quick
fix
add
inferred
type

Suggestion

VS Code should provide a refactoring command for generating a type annotation.

Use Cases

In our code base, we use the TSLint's typedef rule to enforce type annotations. For example:

// TSLINT ERROR
const map = this.getMap();
const value = map.getValue(someKey);

// GOOD
const map: Map<string, CatalogItem> = this.getMap();
const value: CatalogItem = map.getValue(someKey);

It's true that these types can usually be inferred by the compiler. However, requiring people to write explicit annotations makes code more readable, especially in situations where IntelliSense is unavailable. For example: When reviewing a GitHub pull request, or printing a Git history, we get no help from IntelliSense.

Details

VS Code should provide a refactoring command for generating a type annotation. For example, if I right-click on map in this code...

const map = this.getMap();

...then I should be able to do Refactor -> Add Type, and it will convert it to the good form shown above.

Although this is a super easy operation for the compiler engine, I cannot seem to find an implementation of this feature.

I found a couple projects typescript-plugin-add-type and typescript-plugin-proactive-code-fixes. However, they aren't usable:

  • They aren't maintained any more
  • The latest release is incompatible with the latest VS Code
  • They are modeled as compiler plugins

Representing refactoring operations as compiler plugins doesn't seem like the best design:

  • The refactoring engine will malfunction unless the npm install is in a good state, which is not always the case for a project that is "under construction"
  • The plugin will have to be added separately to each project. It would be much more convenient to install it once (e.g. a VS Code extension), and then be able to use it anywhere.

Since the implementation is relatively simple, this seems like a good candidate for a built-in feature.

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 le comportement de refactorisation TypeScript demandé et les exemples de l’issue, puis suivez l’endroit où TypeScript expose les commandes de refactorisation. Définissez comment les annotations inférées doivent être générées pour les déclarations présentées et vérifiez que la commande produit un code annoté équivalent sans modifier le comportement à l’exécution.

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

Évaluation

Stack technique
typescript
Domaine
compilers, developer-experience
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.