microsoft / microsoft/TypeScript

Make Arrays in public API readonly

Offen
#42,481 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Lesen der Deklarationen der öffentlichen API für Node#getChildren, UnionOrIntersectionType#types, TypeChecker#getBaseTypes, TypeChecker#getPropertiesOfType und Symbol#declarations. Suche nach weiteren öffentlichen Array-Eigenschaften und überprüfe die Verwendung von emptyArray. Als abgeschlossen gilt die Aufgabe, wenn die zutreffenden exponierten Arrays readonly sind, während das Laufzeitverhalten von JavaScript unverändert bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
api, compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.