microsoft / microsoft/TypeScript
Using nullish coalescing with untyped field in constructor trigger TS7022
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci il problema con il TypeScript Playground collegato e le classi Node e Children fornite. Traccia la gestione da parte del compilatore dei campi privati senza tipo e del nullish coalescing in relazione a TS7022; il lavoro è completato quando l'esempio viene compilato e #parent viene inferito come Node | null senza annotazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100