Skip to content

Property 'X' has no initializer and is not definitely assigned in the constructor #61046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AhmedElTabarani opened this issue Jan 25, 2025 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@AhmedElTabarani
Copy link

🔎 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

@MartinJohns
Copy link
Contributor

Duplicate of #59997.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 28, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants