microsoft / microsoft/TypeScript

Readonly shorthand cast for expression where `as const` is forbidden

Offen
#59,726 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 Search Terms

readonly, const readonly, readonly cast, readonly cast expression

✅ Viability Checklist
⭐ Suggestion

Adding a readonly modifier keyword to an expression to cast it automatically to its readonly T counterpart using the T type resulting from the expression.

My main fear is that it could be considered an expression-level syntax and consequently rejected. If this syntax is problematic, is there another possible syntax for providing the same behavior? I have considered as readonly auto, but this auto keyword should then be introduced in TS.

📃 Motivating Example

Assigning a mutable object to a const can lead to subtle bugs (or at least, undesired behaviors) if we expect it to not be modified:

const t = [1, 2, 3] as const
const doubles = t.map(x => x * 2)
doubles.push(4) // OK!

The solution could be to write:

const t = [1, 2, 3] as const
const doubles: readonly number[] = t.map(x => x * 2)
// Or: const doubles = t.map(x => x * 2) as readonly number[]
doubles.push(4) // Error!

But it is more verbose than necessary, since the actual T in the readonly T type could be automatically determined based on the expression.

With this proposal, we could just write:

const t = [1, 2, 3] as const
const doubles = readonly t.map(x => x * 2)
doubles.push(4) // Error!

To cast the expression as its readonly equivalent type.

💻 Use Cases

The main goal is to make converting a type to its readonly counterpart easy to write, to make trivial expressing the appropriate type for const-assigned objects to be truly immutable when it is relevant.

The actual solution works, but is too verbose, since it requires to explicitly write the full type of the expression, making it very undesirable to use, and therefore unlikely to be.

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

Es sind keine Implementierungsdateien, Tests oder Einstiegspunkte angegeben. Beginne mit der Prüfung der vorgeschlagenen readonly-Ausdruckssyntax und ihrer Alternativen und bestimme anschließend, ob die Funktion mit den Designbeschränkungen von TypeScript vereinbar ist; als abgeschlossen gilt die Aufgabe, wenn eine akzeptierte Syntax und ein Implementierungsplan mit den entsprechenden Tests vorliegen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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