microsoft / microsoft/TypeScript

Make Arrays in public API readonly

Abierto
#42,481 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

🔍 Search Terms

API readonly
getchildren readonly

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

This might actually break users of TypeScript's API. But so did the last change to make all properties on Node readonly.
Ideally users only need to adjust their types or create a slice of the array.

⭐ Suggestion

Node#getChildren() should return a readonly Node[].
UnionOrIntersectionType#types should be readonly Type[]
TypeChecker#getBaseTypes(type) should return readonly BaseType[] | undefined
TypeChecker#getPropertiesOfType(type) should return readonly Symbol[]
Symbol#declarations should be readonly Declaration[], well actually it should be readonly Declaration[] | undefined, but that's a different long-standing issue

... just to name a few. There are more throughout the API.

probably a stretch goal: make every property on Type, Symbol, etc. readonly (for API users)

📃 Motivating Example

The current types allow API users to mutate data structures they shouldn't be mutating. This might cause severe misbehavior down the road. E.g. EndOfFileToken#getChildren() might return emptyArray. If I modify this array, everything will break, because TypeScript reuses this array instance literally everywhere.

On a related note: It might make sense to change to const emptyArray: readonly never[]. Though that will probably cause a few (reasonable) breaks in the compiler itself.

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

Empieza leyendo las declaraciones de la API pública de Node#getChildren, UnionOrIntersectionType#types, TypeChecker#getBaseTypes, TypeChecker#getPropertiesOfType y Symbol#declarations. Busca otras propiedades públicas de tipo array y revisa el uso de emptyArray. Se considera terminado cuando los arrays expuestos aplicables sean readonly, mientras el comportamiento de JavaScript en tiempo de ejecución permanezca sin cambios.

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

Evaluación

Stack tecnológico
typescript
Área
api, compilers
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.