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
{{ message }}
Repository navigation
Class static block does not allow forward reference to static method #45932
Compiler error in line 3: "Property 'doSomething' is used before its initialization.(2729)"
The code works as-is in native JavaScript, tested in Firefox Developer Edition.
The rewrites for class static blocks in Babel as well as in TypeScript itself also work. Both execute the static block's code at a time where static methods of the class are already available.
🙂 Expected behavior
No compiler error, because everything's fine.
A forward reference to a static field, however, should be an error (I guess).
Workaround
Fall back to the way class static blocks could be emulated before they were supported:
Define an unused #-private static constant whose initializer is an immediate-evaluating function.
Thank you for adding this to 4.6.0 milestone.
I was just wondering why this still has the "needs investigation" label. Do you doubt this is a bug? Do you need further elaboration?
Issues are currently coming in faster than I can triage them. It looked like a probable bug to me but I know very little about class static blocks. If I were to take the time to be 100% confident that this is a bug, I would not have time to do the things that need to happen to release TypeScript 4.5. On the other hand, Ron will know if this is a bug within about 30 seconds. “Needs investigation” ~= “Looks suspicious but I defer to the assignee’s judgment, because they have more expertise than me, can devote more time to studying it than me, or both.” Sadly that doesn’t fit in a label.
Methods should be accessible. Static fields and static blocks are deferred until the end of ClassDefinitionEvaluation after static and prototype methods are defined (including private static elements).
To motivate that this should really be included in the next milestone: It's even worse!
I just found out that from a static code block, you cannot even reference a constant defined before the class:
constFOO="FOO";classBar{static{console.log(FOO);// Variable 'FOO' is used before being assigned.(2454)}}
This results in the unjustified compile error "Variable 'FOO' is used before being assigned.(2454)", but if you still run it, works as it should, logging "FOO".
For this additional case, I can't even think of a work-around, which makes this bug far more serious.
To motivate that this should really be included in the next milestone: It's even worse!
I just found out that from a static code block, you cannot even reference a constant defined before the class:
constFOO="FOO";classBar{static{console.log(FOO);// Variable 'FOO' is used before being assigned.(2454)}}
This results in the unjustified compile error "Variable 'FOO' is used before being assigned.(2454)", but if you still run it, works as it should, logging "FOO".
For this additional case, I can't even think of a work-around, which makes this bug far more serious.
This is related to #44949. so, you are probably the second one.
Activity
fwienber commentedon Sep 20, 2021
Any first evaluation / labels for this one? Maybe Ron Buckton (@rbuckton) or Zzzen (@Zzzen)?
I hope the example code makes the case quite obvious.
fwienber commentedon Nov 10, 2021
Thank you for adding this to 4.6.0 milestone.
I was just wondering why this still has the "needs investigation" label. Do you doubt this is a bug? Do you need further elaboration?
andrewbranch commentedon Nov 10, 2021
Issues are currently coming in faster than I can triage them. It looked like a probable bug to me but I know very little about class static blocks. If I were to take the time to be 100% confident that this is a bug, I would not have time to do the things that need to happen to release TypeScript 4.5. On the other hand, Ron will know if this is a bug within about 30 seconds. “Needs investigation” ~= “Looks suspicious but I defer to the assignee’s judgment, because they have more expertise than me, can devote more time to studying it than me, or both.” Sadly that doesn’t fit in a label.
rbuckton commentedon Nov 10, 2021
Methods should be accessible. Static fields and static blocks are deferred until the end of ClassDefinitionEvaluation after static and prototype methods are defined (including private static elements).
fwienber commentedon Mar 4, 2022
To motivate that this should really be included in the next milestone: It's even worse!
I just found out that from a static code block, you cannot even reference a constant defined before the class:
Run in TypeScript Playground
This results in the unjustified compile error "Variable 'FOO' is used before being assigned.(2454)", but if you still run it, works as it should, logging "FOO".
For this additional case, I can't even think of a work-around, which makes this bug far more serious.
Zzzen commentedon Mar 6, 2022
You are probably the first person in the world who use class static block with TypeScript.
Zzzen commentedon Mar 6, 2022
This is related to #44949. so, you are probably the second one.
fwienber commentedon Mar 30, 2022
This other Playground example is now also fixed when choosing TypeScript "nightly" (4.70-dev...)!
Thanks again for making it work!