-
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
Computed Properties aren't bound correctly during Object/Class evaluation #27864
Comments
There's a type error on the line with the computed property:
However
|
Taking PRs and marking it as moderate (assuming you are familiar with spec-reading and the transform pipeline). |
What are the implications of getting rid of the type error? Will allowing arbitrary string and symbol values in computed field declarations affect inference? |
Type errors aside, here's one possible emit that works correctly. I haven't seen any other cases of invoking an IIFE with parameters like this. Are there any?
|
Maybe we can lean on the iife stuff that's already done to transpile The currently generated code for Joey's example when targeting ES2015 is: var _a, _b;
"use strict";
const classes = [];
for (let i = 0; i <= 10; ++i) {
classes.push((_b = class A {
constructor() {
this[_a] = "my property";
}
},
_a = i,
_b));
}
for (const clazz of classes) {
console.log(Object.getOwnPropertyNames(new clazz()));
} If we move "use strict";
const classes = [];
for (let i = 0; i <= 10; ++i) {
let _a, _b; // <---- was moved
classes.push((_b = class A {
constructor() {
this[_a] = "my property";
}
},
_a = i,
_b));
}
for (const clazz of classes) {
console.log(Object.getOwnPropertyNames(new clazz()));
}
|
the special temporary variable and hoist is happened in ts transformer before es2015 |
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
@Kingwl agreed. |
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
* Fix microsoft#27864 Signed-off-by: Joseph Watts <jwatts43@bloomberg.net> * Tests for computed field scope fix Signed-off-by: Joseph Watts <jwatts43@bloomberg.net>
I noticed that there's also an issue with the binding of property declaration initializers in class expressions which are within an iteration statement. For example: let arr = [];
for (let i = 0; i < 5; ++i) {
arr.push(class C {
test = i;
})
}
arr.forEach(clazz => console.log(new clazz().test)); Since the I've investigated the changes required to the checker in order to pick up this binding, and it appears to me that a simple change to the |
TypeScript Version: 3.2.0-dev.20181011
Search Terms: computed property expression
Code
Expected behavior: The log statements indicate that each class in
classes
has a different property name (i
should be evaluated at the time of the class evaluation and all instances of that class should have a property name corresponding to that evaluation ofi
).Actual behavior: Compiled code logs:
Playground Link
The text was updated successfully, but these errors were encountered: