microsoft / microsoft/TypeScript

Allow `readonly` with `accessor`

Aperta
#55,289 5 commenti 7 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔍 Search Terms

accessor
decorator
readonly

✅ Viability Checklist
  • 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

Add readonly as a modifier for accessor fields.

📃 Motivating Example

Suppose you have an injection framework with @provides, @consumes, and @inject. Consider the example

class A {
  @provides(Number)
  readonly value: number;

  @inject([Number])
  accessor b = new B();

  constructor(value: number) {
    this.value = value;
  }
}

class B {
  @consumes(Number)
  readonly value: number;
}

Note because @provides is a field decorator, it cannot know when value is assigned unless it's in the initializer. As a consequence, b will not have value injected since @provides doesn't know when to reinject. This is fixed if you do

class A {
  @provides(Number)
  readonly accessor value: number;

  @inject([Number])
  accessor b = new B();

  constructor(value: number) {
    this.value = value;
  }
}

class B {
  @consumes(Number)
  readonly value: number;
}
💻 Use Cases
  1. What do you want to use this for?
    Accessor declarations that shouldn't be modified after being used in the constructor.
  2. What shortcomings exist with current approaches?
    You can only mark a field with @readonly using JSDoc and just hope that other developers don't touch the field.
  3. What workarounds are you using in the meantime?
    @readonly using JSDoc

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

Nell’issue non sono identificati file sorgente, test o punti di ingresso dell’implementazione. Inizia tracciando come vengono analizzate e verificate le dichiarazioni degli accessor e i modificatori readonly, quindi aggiungi una copertura che dimostri che le dichiarazioni degli accessor readonly sono accettate e non possono essere riassegnate.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.