microsoft / microsoft/TypeScript
Allow `readonly` with `accessor`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔍 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
- What do you want to use this for?
Accessor declarations that shouldn't be modified after being used in the constructor. - What shortcomings exist with current approaches?
You can only mark a field with@readonlyusing JSDoc and just hope that other developers don't touch the field. - What workarounds are you using in the meantime?
@readonlyusing JSDoc
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Im Issue sind keine Quelldateien, Tests oder Einstiegspunkte für die Implementierung angegeben. Beginne damit nachzuverfolgen, wie Accessor-Deklarationen und readonly-Modifikatoren geparst und geprüft werden, und füge anschließend Abdeckung hinzu, die zeigt, dass readonly-Accessor-Deklarationen akzeptiert werden und ihnen kein neuer Wert zugewiesen werden kann.
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
- 35/100