microsoft / microsoft/TypeScript
Using nullish coalescing with untyped field in constructor trigger TS7022
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
Bug Report
Using nullish coalescing in a constructor to initialize a class member with no type annotation will result in a TS7022 error.
🔎 Search Terms
nullish, coalescing, 7022, ??, and ts7022
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing and type inference.
⏯ Playground Link
Playground link with relevant code
💻 Code
export class Node {
#parent
#children = new Children(this)
constructor (parent?: Node | null | undefined) {
// Remove `??` and the code after and the code compiled. Now #parent is undefined which is not desired.
this.#parent = parent ?? null
}
get parent () { return this.#parent }
get children () { return this.#children }
}
export class Children {
#parent
#head
#tail
constructor (parent: Node) {
this.#parent = parent
this.#head = new Node(parent)
this.#tail = new Node(parent)
}
}
🙁 Actual behavior
Attempting to compile the example code results in a TS7022 error: '#parent' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
🙂 Expected behavior
The code should compile and #parent should be typed as Node | null.
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 Problem mit dem verlinkten TypeScript Playground und den bereitgestellten Klassen Node und Children. Verfolge, wie der Compiler untypisierte private Felder und Nullish Coalescing im Zusammenhang mit TS7022 behandelt; abgeschlossen ist die Aufgabe, wenn das Beispiel kompiliert und #parent ohne Annotation als Node | null inferiert wird.
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
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100