-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Using object destructuring with ECMAScript's private field as computed property name leads to runtime error #37791
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
Comments
The following example works for the "use strict";
class Foo {
constructor(value) {
this.#value = value;
}
#value; // <- declarated private value. if remove it JavaScript engines will throw
// Private field '#propertyName' must be declared in an enclosing class
getValue(data) {
const { [this.#value]: value } = data;
return value;
}
} You can try to use const key = this.#value;
const { [key]: value } = data; However, I think, it is a bug, because, the following code const { [this.#value]: value } = data; should be converted something like so const { [__classPrivateFieldGet(this, _value)]: value } = data; |
I was going to open a new issue, but I think it's the same as this one. However, I'm using computed class methods and not destructuring: let getX: (a: A) => number;
class A {
#x = 2;
[(getX = (obj: A) => obj.#x, "_")]() {}
}
console.log(getX!(new A)); |
@nicolo-ribaudo The fix @a-tarasyuk has proposed doesn't address this, but I think I found the cause of the issue you're seeing. I've brought up the problem in the PR so hopefully we can resolve both at the same time. |
@nicolo-ribaudo I've turned your code sample into a repro the bot can track: // @showEmit
// @target: ES2015
let getX: (a: A) => number;
class A {
#x = 2;
[(getX = (obj: A) => obj.#x, "_")]() {}
}
getX!(new A); |
Same for the repro mentioned by @a-tarasyuk: // @showEmit
// @target: ES2015
class Foo {
#value: any;
constructor(value: any) {
this.#value = value;
}
getValue(data: any) {
const { [this.#value]: value } = data;
return value;
}
} |
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the 2 repros in this issue running against the nightly TypeScript. If something changes, I will post a new comment. Comment by @rbuckton 👍 Compiled "use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var _value;
class Foo {
constructor(value) {
_value.set(this, void 0);
__classPrivateFieldSet(this, _value, value);
}
getValue(data) {
const { [(_value = new WeakMap(), this.#value)]: value } = data;
return value;
}
} 👍 Compiled "use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var _x;
let getX;
class A {
constructor() {
_x.set(this, 2);
}
[(_x = new WeakMap(), (getX = (obj) => obj.#x, "_"))]() { }
}
getX(new A); Historical InformationComment by @louistio
Comment by @louistio
|
@typescript-bot run repros |
Heya @orta, I've started to run the code sample repros for you. Here's the link to my best guess at the log. |
TypeScript Version: 3.8.3
Search Terms: computed object property name destructuring private ECMAScript
Code
Expected behavior:
'Adam' is logged at runtime.
Actual behavior:
Runtime error occurs:
Additional information:
I just started using TypeScript and I ran into this problem while converting one of my javascript projects, the behavior was confusing to me. It seems like the code is transpiled incorrectly, as it works when not using destructuring syntax:
The error does not occur when using TypeScript's
private
field instead of ECMAScript's.The error does not occur when targeting ESNext.
Related Issues: Maybe #26572 #26355? Doesn't seem like the same problem but similar keywords.
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: