Skip to content

Static class field allows colon value with no assignment #56837

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
TrueWill opened this issue Dec 20, 2023 · 3 comments
Closed

Static class field allows colon value with no assignment #56837

TrueWill opened this issue Dec 20, 2023 · 3 comments

Comments

@TrueWill
Copy link

πŸ”Ž Search Terms

static

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about static

⏯ Playground Link

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

πŸ’» Code

class Foo {
    static bar: true;
}

console.log(Foo.bar); // undefined

πŸ™ Actual behavior

I spotted this in production code.

The developer intended to write

class Foo {
    static bar = true;
}

which makes sense. TypeScript gives no errors or warnings with the colon-value. While you can use a colon to specify a type when assigning a value, that does not seem to be what is happening here. I can see no reason why anyone would intentionally write the original code.

πŸ™‚ Expected behavior

TypeScript reports an error.

Additional information about the issue

No response

@fatcerberus
Copy link

This technically may not a bug but a missing feature. In any case, duplicate of #27899.

@fatcerberus
Copy link

fatcerberus commented Dec 21, 2023

I can see no reason why anyone would intentionally write the original code.

It can sometimes make sense to write code like this, if you intend to do the actual initialization in a static block:

class Foo {
    static bar: boolean;
    static {
        this.bar = isBar();
    }
}

Note that true is a valid literal type; in fact boolean is equivalent to true | false.

@TrueWill
Copy link
Author

Closing as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants