microsoft / microsoft/TypeScript

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

Aperta
#59,726 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔍 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.

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

Non sono stati identificati file di implementazione, test o punti di ingresso. Inizia esaminando la sintassi proposta per le espressioni readonly e le relative alternative, quindi determina se la funzionalità è compatibile con i vincoli di progettazione di TypeScript; il lavoro è completo quando sono disponibili una sintassi accettata e un piano di implementazione con i test corrispondenti.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.