🔎 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
https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250125#code/IYIwzgLgTsDGEAJYBthjAgkgEwOIFMA7fGCAeygH0BGBAbwCgFmEAHKASwDdgJ8FIvDrASpIOAFwJCAVwC2IEggC8CAAwBuJi3bde-DtimyFJDQgD0FhAFEoUCg23NYZQpCgz4FABQBKemcWZggACw4wADowfAgCYlJ8bBx-LWCAXydg3R4+AVj4kn1k7H9A4OCcQtIKGkixCBwAaia0ipDwqMMVLDwiIvIqanq0Ruw25kyg1hkQZGEEAHNYlL9jeUUocvaEKFiZKEIEMIjIwwmETKnQDzhEFDQMKv6aqgAmbeYc-QEIIREGpJpBslKpNNNOLkDEZgaYoOYrAgAHJkWz2RxBVzuaBeQZlRg7XrVXi1N4jcTYFoXYInLrYHrPBIk97ksYXKbZWbzETLMb+dZwz4VPYQA5HWlncZBK5AA
💻 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
🔎 Search Terms
Property 'X' has no initializer and is not definitely assigned in the constructor
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250125#code/IYIwzgLgTsDGEAJYBthjAgkgEwOIFMA7fGCAeygH0BGBAbwCgFmEAHKASwDdgJ8FIvDrASpIOAFwJCAVwC2IEggC8CAAwBuJi3bde-DtimyFJDQgD0FhAFEoUCg23NYZQpCgz4FABQBKemcWZggACw4wADowfAgCYlJ8bBx-LWCAXydg3R4+AVj4kn1k7H9A4OCcQtIKGkixCBwAaia0ipDwqMMVLDwiIvIqanq0Ruw25kyg1hkQZGEEAHNYlL9jeUUocvaEKFiZKEIEMIjIwwmETKnQDzhEFDQMKv6aqgAmbeYc-QEIIREGpJpBslKpNNNOLkDEZgaYoOYrAgAHJkWz2RxBVzuaBeQZlRg7XrVXi1N4jcTYFoXYInLrYHrPBIk97ksYXKbZWbzETLMb+dZwz4VPYQA5HWlncZBK5AA
💻 Code
I encountered an inconsistency in the TypeScript compiler when using static properties in abstract classes
The following code produces an error in
IdGenerator_1but not inIdGenerator_2🙁 Actual behavior
The compiler produces an error for
IdGenerator_1but not forIdGenerator_2🙂 Expected behavior
Both
IdGenerator_1andIdGenerator_2should compile without errors, as they are functionally identicalAdditional information about the issue
No response