Closed as not planned
Closed as not planned
Description
🔎 Search Terms
Property 'X' has no initializer and is not definitely assigned in the constructor
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
I encountered an inconsistency in the TypeScript compiler when using static properties in abstract classes
The following code produces an error in IdGenerator_1
but not in IdGenerator_2
abstract class IdGenerator_1 {
private static lastId: number = 0;
private id: number; // Error
constructor() {
this.setGeneratedId();
}
private setGeneratedId() {
IdGenerator_1.lastId++;
this.id = IdGenerator_1.lastId;
}
public getId(): number {
return this.id;
}
}
abstract class IdGenerator_2 {
private static lastId: number = 0;
private id: number; // No Error
constructor() {
IdGenerator_2.lastId++;
this.id = IdGenerator_2.lastId;
}
public getId(): number {
return this.id;
}
}
🙁 Actual behavior
The compiler produces an error for IdGenerator_1
but not for IdGenerator_2
🙂 Expected behavior
Both IdGenerator_1
and IdGenerator_2
should compile without errors, as they are functionally identical
Additional information about the issue
No response