You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"no initializer and is not definitely assigned in the constructor", computed property name, const string property name, symbol class field, enum class field
consttwo="two";enumOtherProps{three="three",four="four"}classFoo{// 👍 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.
The text was updated successfully, but these errors were encountered:
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
🙁 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 isstring
.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 forone
(getProp()
could return"one"
)? If we're willing to accept that sort of potential for false-positives for theone
case, it should be equally acceptable risk for the other cases as well.The text was updated successfully, but these errors were encountered: