microsoft / microsoft/TypeScript
"Used before initialization" error false positives and negatives with optional properties
@johnfav03 ci sta già lavorando.
Dal 6/8/2025.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
🔎 Search Terms
used before its initialization
🕗 Version & Regression Information
- This changed between versions 4.2.3 and 4.3.5
- This changed in commit or PR #43100 (
0967f3b)
⏯ Playground Link
💻 Code
class C {
a = this.b;
b?: number = 123;
}
class D {
a: number | undefined;
b = this.a = 123;
}
🙁 Actual behavior
When initializing C.a: no error.
When initializing D.b:
Property 'a' is used before its initialization. (2729)
🙂 Expected behavior
When initializing C.a:
Property 'b' is used before its initialization. (2729)
When initializing D.b: no error.
Additional information about the issue
Tried with both useDefineForClassFields set to true and false.
The behavior is as expected when switching the use of undefined and the "optional" ? modifier like this:
class C {
a = this.b;
b: number | undefined = 123;
}
class D {
a?: number;
b = this.a = 123;
}
#43100 seems to have taken a wrong approach. Instead of basing the decision to report an error on whether the ? modifier is present, it should be based on whether the property access is on the left-hand side of an assignment expression.
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.
Valutazione
Questa issue non è ancora stata valutata.