microsoft / microsoft/TypeScript

Make Arrays in public API readonly

Aperta
#42,481 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

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

Inizia leggendo le dichiarazioni dell’API pubblica per Node#getChildren, UnionOrIntersectionType#types, TypeChecker#getBaseTypes, TypeChecker#getPropertiesOfType e Symbol#declarations. Cerca altre proprietà pubbliche di tipo array e verifica l’utilizzo di emptyArray. Il lavoro è completato quando gli array esposti applicabili sono readonly, mentre il comportamento di JavaScript in fase di esecuzione rimane invariato.

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

Valutazione

Stack tecnologico
typescript
Ambito
api, compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.