microsoft / microsoft/TypeScript

Refactoring: Generate type annotation from inferred type

Abierto
#32,319 5 comentarios 16 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Domain: LS: Refactorings Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el comportamiento de refactorización de TypeScript solicitado y los ejemplos del issue; después, sigue el recorrido hasta donde TypeScript expone los comandos de refactorización. Define cómo deben generarse las anotaciones inferidas para las declaraciones mostradas y verifica que el comando produzca código anotado equivalente sin cambiar el comportamiento en tiempo de ejecución.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers, developer-experience
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
30/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.