microsoft / microsoft/TypeScript
Double initializing a `readonly` field should not be allowed
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 Search Terms
readonly double twice initializer
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about initializers?
⏯ Playground Link
💻 Code
class Foo {
readonly bar: string = 'initializer 1';
constructor() {
this.bar = 'initializer 2';
}
}
console.log(new Foo().bar);
🙁 Actual behavior
This compiles successfully despite overwriting a readonly field. This breaks expectations around readonly. If Foo happens to be a very large class and I only look at readonly bar: string = 'initializer 1'; in isolation, I would expect any other line which reads bar to see initializer 1. However this is not a safe assumption because I need to check in the constructor to ensure it doesn't overwrite my readonly field.
🙂 Expected behavior
I expected a compiler error. TS should only allow initializing readonly fields in a constructor, not overwriting a field which has already been initialized.
Additional information about the issue
I get that we need readonly fields to be assignable in the constructor, but it feels like a bug to me that this is allowed when the field has already been initialized prior to the constructor. readonly should require a field initializer xor a constructor initializer. Having both is invalid IMHO.
You can make the same argument that double initializing in the constructor should be invalid too:
class Foo {
readonly bar: string;
constructor() {
this.bar = 'initializer 1';
this.bar = 'initializer 2'; // Should maybe error? Doesn't today.
}
}
But I understand that control flow analysis gets a lot more complicated in the constructor WRT readonly and might be considered a different feature with different priority.
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
Beginne damit, die verknüpften TypeScript Playground-Beispiele auszuführen, und vergleiche den Fall mit dem Feldinitialisierer mit dem Fall der doppelten Zuweisung nur im Konstruktor. Die Änderung ist abgeschlossen, wenn das erste Beispiel einen Compilerfehler für die Zuweisung an das bereits initialisierte readonly-Feld erzeugt und das gewünschte Verhalten durch einen Regressionstest abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100