Skip to content
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

strictPropertyInitialization does not check enum/const computed property names #42429

Closed
peterflynn opened this issue Jan 21, 2021 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@peterflynn
Copy link

peterflynn commented Jan 21, 2021

Bug Report

🔎 Search Terms

"no initializer and is not definitely assigned in the constructor", computed property name, const string property name, symbol class field, enum class field

🕗 Version & Regression Information

Seen in: 3.3.3, 3.9.7, and 4.1.3

⏯ Playground Link

Playground link for the code below

💻 Code

const two = "two";

enum OtherProps {
    three = "three",
    four = "four"
}

class Foo {
    // 👍 ERROR: Property 'one' has no initializer and is not definitely assigned in the constructor.
    one: string;

    // 🙁 No error
    [two]: string;

    // 🙁 No error
    [OtherProps.three]: string;
}

🙁 Actual behavior

The "two" and "three" properties are accepted without error, even though they're just as incorrect (never initialized) as "one."

🙂 Expected behavior

The "strictPropertyInitialization" error should trigger for all three cases -- since the property name is statically known at compile time for all three, they're all equally able to be checked.

When computed properties use a more complex expression that can't be resolved at compile time, it's understandable that TypeScript can't reason about it as effectively. But the cases above are statically understood, to the point where you can access them via regular identifiers elsewhere: if you write new Foo().two or .three, its type is string.

It's true that strictPropertyInitialization could give false positive errors by missing places where they are initialized via more dynamic indexing (e.g. foo[getProp()] =""). But isn't that equally true for one (getProp() could return "one")? If we're willing to accept that sort of potential for false-positives for the one case, it should be equally acceptable risk for the other cases as well.

@peterflynn
Copy link
Author

Does this have a similar root cause as limitations like #25758?

@MartinJohns
Copy link
Contributor

Duplicate of #36302. Used search terms: strictpropertyinitializatiom computed

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

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

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