microsoft / microsoft/TypeScript
emit an error when using #private fields in their "temporal dead zone"
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
error for private member accessed before ready
✅ 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
This produces a runtime error when you run it:
abstract class Base {
constructor() {
this.createStuff()
}
abstract createStuff(): void
}
class Sub extends Base {
#makeSomething() {
console.log('make stuff')
}
createStuff() {
this.#makeSomething()
}
}
// TypeError: Cannot read private member from an object whose class did
// not declare it (JavaScript points finger at you while laughing)
new Sub().createStuff()
📃 Motivating Example
It has happened to me too many times due to subclass code running early before initialization of private fields.
Unfortunately the simple solution is to avoid class fields for this case (use properties-defined-in-constructor, or getters).
💻 Use Cases
Sometimes we want a constructor to make stuff generically for subclasses, but then subclasses should override or implement some methods to replace certain parts.
I believe there is enough static info that it would be possible to check for this case and emit a compile error.
Similarly it would also be possible to enforce non-private fields to be | undefined when used early in a location that is known to be called before initialization (f.e. in a superclass constructor).
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
Reproduziere das Unterklassenbeispiel im verlinkten TypeScript Playground und vergleiche seinen Laufzeitfehler mit der angeforderten Compile-Time-Diagnose. Bestimme den vorgesehenen Umfang für den Zugriff des Superklassenkonstruktors auf private und nicht-private Felder, und definiere anschließend Tests, die bestätigen, dass die relevanten Fälle einer frühzeitigen Verwendung abgelehnt werden, ohne das ausgegebene JavaScript zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100