microsoft / microsoft/TypeScript
TS2333: 'this' cannot be referenced in constructor arguments
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
Suggestion
✅ Viability Checklist
My suggestion meets these guidelines:
- 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 TypeScript's Design Goals.
⭐ Suggestion
TypeScript disallows this:
class A {
constructor(readonly b: B = new B(this)) {}
}
class B {
constructor(a: A) {}
}
TS2333: 'this' cannot be referenced in constructor arguments
Instead, we can rewrite it as follows.
class A {
readonly b: B;
constructor(b?: B) {
this.b = b ?? new B(this);
}
}
class B {
constructor(a: A) {}
}
However, this is clearly more verbose. Since this transformation is always possible, it seems TypeScript should allow this in the constructor and be doing the job of rewriting it for us, so we can maintain simpler code using the former constructor argument approach.
📃 Motivating Example
Permitting this references in constructor arguments simplifies object initialization.
💻 Use Cases
Creating classes at construction time that reference the creating class.
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
Beginnen Sie damit, die TS2333-Diagnose und die Prüfungen des Compilers für this in Konstruktorargumenten nachzuverfolgen; das Issue nennt keine bestimmten Dateien oder Tests. Vergleichen Sie die vorgeschlagenen Konstruktorbeispiele mit dem erzeugten JavaScript und dem bestehenden Verhalten der Typprüfung, und ziehen Sie Tests in Betracht, die festlegen, wann die Referenz akzeptiert werden sollte, ohne die Laufzeitausgabe zu ändern.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100