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
Statements in class static blocks do not narrow types #44949
class Foo {
static {
Foo.doSomething(); // Error: "Property 'doSomething' is used before its initialization.(2729)"
}
// this is how Babel rewrites the above:
static #_ = (() => {
Foo.doSomething(); // Now, the forward reference is allowed!
})();
static doSomething(): void {
console.log("gotcha!");
}
}
This code on TypeScript Playground
Are class static blocks supposed to be more strict in this regard?
The Babel rewrite for class static blocks is to define an unused, #private static variable whose initializer is a self-evaluating arrow function. Using the rewrite, TypeScript does not complain about the forward reference.
At runtime, both variants work.
It seems to me that the TypeScript error should not be raised in this case.
Is this the same problem as above or should I create a separate issue?
Activity
tchetwin commentedon Jul 14, 2021
I accidentally duplicated this in #45023. Carrying over a point from there:
"Side effects" in initialization of static fields already behaved similarly (since ~forever):
fwienber commentedon Sep 13, 2021
What about this even simpler case:
This code on TypeScript Playground
Are class static blocks supposed to be more strict in this regard?
The Babel rewrite for class static blocks is to define an unused, #private static variable whose initializer is a self-evaluating arrow function. Using the rewrite, TypeScript does not complain about the forward reference.
At runtime, both variants work.
It seems to me that the TypeScript error should not be raised in this case.
Is this the same problem as above or should I create a separate issue?
Zzzen commentedon Sep 14, 2021
I believe that's a separate issue.