-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
useDefineWithClassFields should use-before-init error when class property initializer refers to parameter property #50971
Comments
Is |
In newer targets, |
It’s impossible for |
The code generation here is as-expected; the bug is the lack of error message to flag the use-before-init. |
Not directly related to the issue but if |
Parameter properties are considered to be sugar for a |
Doesn't it reduces readability? I was so happy TypeScript handles initialization ordering in constructor for me. |
It looks like our expectation (prior to constructor(facade) {
this.facade = facade;
this.bug = this.facade.create();
console.log(this.bug);
} I would contend that the issue is our emit when facade;
bug;
constructor(facade) {
this.facade = facade;
this.bug = this.facade.create();
console.log(this.bug);
} |
FWIW, babel also produces code where this is a runtime error. |
Hi, anything guys? I also faced that, to say more we have ngrx code, which declares multiple effects in a class based on received in the constructor values, but these declared properties are not declared in the constructor. That is overkill. From other fields, like kotlin or scala - you have a constructor, and later you can use these values from the constructor, to create other properties. That should be obvious, and IMO it was for many users before ES2022. |
Removes additional commands from branches/branch nodes in closed repos Adds better typing for view commands Avoids use-before-init w/ useDefineForClassFields See microsoft/TypeScript#50971
Bug Report
This bug exists in my project, not only playground, but playground seem to also have bugs, because i can't even seem to reliably reproduce it in the playground – sometimes it does not react on
Target
change. But right now i have an opened playground tab with this bug.I will duplicate the MRE code right here as a text.
This is the source code
This is the compiled code with
target: ES2022
andmodule: CommonJS
If run this JS – we will get TypeError
⏯ Playground Link
Playground link with relevant code
I'm also sharing the playground link, but i assume it could produce the right code for you if you open it first time.
To reliable reproduce – change
module
toNode16
and then toCommonJS
. You should see this resultThe text was updated successfully, but these errors were encountered: