Static variable may be undefined when constructing an object of the same class #43285
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Bug Report
I'm not sure whether this may be a issue that can be fixed or just keep it as is "by design". I haven't found any other issues similar to this one here, at least with the search terms I tried (sorry if someone else already reported this 🙂).
🔎 Search Terms
static, undefined, uninitialized, constructor
🕗 Version & Regression Information
At the moment of writing this, TypeScript version is 4.2.3.
⏯ Playground Link
Playground Link
💻 Code
🙁 Actual behavior
When calling the constructor to initialize the static variable
instance
,A.constant
is not yet initialized (it's undefined), so the constructor logsundefined, NaN
.In addition,
A.constant.toString()
gives a javascript execution error, which even in strict mode the compiler doesn't catch.🙂 Expected behavior
The compiler could tell that
constant
has typenumber | undefined
at that moment (manually changing the type ofconstant
tonumber | undefined
correctly yields a compiler error) or even rearrange the generated javascript to initialize firstconstant
and theninstance
(which I think it may be more difficult in more complex scenarios).Thank you very much!
The text was updated successfully, but these errors were encountered: