Skip to content

Issue error if readonly static property has no initializer #43897

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
Trias opened this issue Apr 30, 2021 · 3 comments
Closed

Issue error if readonly static property has no initializer #43897

Trias opened this issue Apr 30, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Trias
Copy link

Trias commented Apr 30, 2021

Bug Report

πŸ”Ž Search Terms

static property readonly undefined typecheck literal

πŸ•— Version & Regression Information

As far as i know this is the behavior of all typescript version, but i think it's not intuitive

⏯ Playground Link

See my Playground example:

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwDYEMDOa4BVhpgYWimAXgG8BYAKDlrjxRgEsE5iUATCAOyQE96EALbAAcihFwAvHABEaYWInBZAbmoBfatVCRYiVBmy4YAISgQA1sG5xKNOg2at2XXgIUjxIgFxzPSiJqmtpUOHiEUMSkAHQB3sDqVNRMAGZwABThZhbW3HGKCdIy8oXKsgCU9nSIPApIwDFIEADmGQAGAK4QADRwTADkQnCd3OwIABYoAEYNMXAAgkgKcDATwKt8YBsQ6dnmVjYFXsr9mAOlJ0EDfQDu68T98GgTEJ1IHHDTG4xwDejwAZ4KBMbgtOAAHxG3A4wFSoOAHAGcGgbEYDzgA1GsPh3ERA3aFU0QA

πŸ’» Code

export class TestCorrect {
    static readonly someName = "someName";
}

export class TestBroken {
     static readonly someName: "someName"; 
     //  spot the error? ----^  
     // note how similar the syntax looks to the class above. 
     // but this time we are declaring a literal type and also prevent any assignment. which makes little sense and is likely a bug. 
     // what was meant is probably the code above.
}

if (TestBroken.someName == "someName") {
    console.log(`uo, i'm unreachable. Also the type of TestBroken.someName is `"someName"`, where it should rather be `undefined` or an error at the class definition)
}

πŸ™ Actual behavior

the code is accepted by typescript

πŸ™‚ Expected behavior

either an error at TestBroken-class or a type inference of "someName" | undefined (first option preferred)

@Trias
Copy link
Author

Trias commented Apr 30, 2021

I'm willing to invest some time in this if you agree that it's a bug and how it should be fixed :)

@MartinJohns
Copy link
Contributor

MartinJohns commented Apr 30, 2021

Duplicate of #27899. At least when ignoring the read-only.

@Trias
Copy link
Author

Trias commented Apr 30, 2021

@MartinJohns thanks! I think for the mutable version it would be hard for typescript to determine if the property gets initialized somewhere, but it could be typed as "literal" | undefined in that case. And if strictPropertyInitialization is enabled or readonly is specified it should require a static assignment in my opinion.

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created In Discussion Not yet reached consensus and removed Duplicate An existing issue was already created labels Apr 30, 2021
@RyanCavanaugh RyanCavanaugh changed the title static readonly property is not typechecked Issue error if readonly static property isn't initialized Apr 30, 2021
@RyanCavanaugh RyanCavanaugh changed the title Issue error if readonly static property isn't initialized Issue error if readonly static property has no initializer Apr 30, 2021
@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus labels Jun 24, 2021
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

3 participants