-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Emit defineProperty calls before param prop assignments #34987
Conversation
Note that I restricted this to --useDefineForClassFields is true. Nothing changes when it's off. I think this is the correct fix for a patch release. However, in principal there's nothing wrong with moving parameter property initialisation after property declaration initialisation. It would be Extremely Bad and Wrong to rely on this working: ```ts class C { p = this.q // what is q? constructor(public q: number) { } } ``` But today it does, and probably somebody relies on it without knowing.
👋 a lot of code in my current project relies on this behavior. |
@ajafff could it be in the spec? I'll have to take a look. If that's the case the long-term fix would need to be an interleaving of parameter property defineProperty/assignments, all happening before the defineProperty calls for other property declarations. |
Or, after discussing with @RyanCavanaugh, removing the assignments and using the parameter as the initial |
That'd work as a quick fix for now, but I'd like to raise an issue with the semantics of parameter properties to begin with. The way it's specified for TypeScript is insufficiently clear, and the most direct interpretation of it actually means it should, in your first I wrote this in a bug report that got closed as a duplicate:
But for the spec issue itself and how to handle it going forward, should I open a new issue? This is important for |
@Jessidhia I think the right thing is to have Although this is complex enough that perhaps we should have a new issue to find out if we need changes to accomodate the class fields proposal. |
@rbuckton mind taking a look at this? This is the fix we talked about yesterday @Jessidhia We decided that we won't make changes to parameter properties unless the TC39 committee considers a similar feature. The feature as specified is kind of weird and fits badly with the ECMAScript spec, but changing it will certainly break people. I don't think it's worthwhile to improve it unless we're forced to. |
Does that mean you always need to use |
🔔 reviewers can we get some eyes on this so we can get 3.7.3 out sooner? |
@typescript-bot cherry-pick this to release-3.7 |
Component commits: 5810765 Emit defineProperty calls before param prop assignments Note that I restricted this to --useDefineForClassFields is true. Nothing changes when it's off. I think this is the correct fix for a patch release. However, in principal there's nothing wrong with moving parameter property initialisation after property declaration initialisation. It would be Extremely Bad and Wrong to rely on this working: ```ts class C { p = this.q // what is q? constructor(public q: number) { } } ``` But today it does, and probably somebody relies on it without knowing. ec79590 Put parameter property initialiser into defineProperty's value
Hey @DanielRosenwasser, I've opened #35242 for you. |
@@ -0,0 +1,20 @@ | |||
// @target: esnext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just merge this with definePropertyES5.ts
(and rename it?) and use this:
// @target: es5, esnext
@typescript-bot cherry-pick this into release-3.7 |
Component commits: 5810765 Emit defineProperty calls before param prop assignments Note that I restricted this to --useDefineForClassFields is true. Nothing changes when it's off. I think this is the correct fix for a patch release. However, in principal there's nothing wrong with moving parameter property initialisation after property declaration initialisation. It would be Extremely Bad and Wrong to rely on this working: ```ts class C { p = this.q // what is q? constructor(public q: number) { } } ``` But today it does, and probably somebody relies on it without knowing. ec79590 Put parameter property initialiser into defineProperty's value be86355 Merge branch 'master' into fix-defineProperty-parameter-property-emit 8ff59b9 Combine ES5/ESNext into one test
Component commits: 5810765 Emit defineProperty calls before param prop assignments Note that I restricted this to --useDefineForClassFields is true. Nothing changes when it's off. I think this is the correct fix for a patch release. However, in principal there's nothing wrong with moving parameter property initialisation after property declaration initialisation. It would be Extremely Bad and Wrong to rely on this working: ```ts class C { p = this.q // what is q? constructor(public q: number) { } } ``` But today it does, and probably somebody relies on it without knowing. ec79590 Put parameter property initialiser into defineProperty's value be86355 Merge branch 'master' into fix-defineProperty-parameter-property-emit 8ff59b9 Combine ES5/ESNext into one test
Note that I restricted this to when --useDefineForClassFields is true. Nothing changes when it's off. I think this is the correct fix for a patch release.
However, in principal there's nothing wrong with moving parameter property initialisation after property declaration initialisation. It would be Extremely Bad and Wrong to rely on this working:
But today it emits the below code, and probably somebody relies on it without knowing:
Fixes #34942