microsoft / microsoft/TypeScript

Refactoring: Generate type annotation from inferred type

Aperta
#32,319 5 commenti 16 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Domain: LS: Refactorings Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Parti dal comportamento di refactoring TypeScript richiesto e dagli esempi nell’issue, quindi segui il percorso fino al punto in cui TypeScript espone i comandi di refactoring. Definisci come devono essere generate le annotazioni inferite per le dichiarazioni mostrate e verifica che il comando produca codice annotato equivalente senza modificare il comportamento a runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers, developer-experience
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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.