We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: Nightly
Search Terms: logical assignment side effect
Code
This code is from lgcl-nullish-assignment-operator-lhs-before-rhs.js, a test262 conformance test.
var assert = { sameValue(a, b, error) { if (a !== b) throw new Error(`${error}: ${a} !== ${b}`) } } var count = 0; var obj = {}; function incr() { return ++count; } assert.sameValue(obj[incr()] ??= incr(), 2, "obj[incr()] ??= incr()"); assert.sameValue(obj[1], 2, "obj[1]"); assert.sameValue(count, 2, "count");
Expected behavior: This code should exit cleanly
Actual behavior: This code crashes with Uncaught Error: obj[incr()] ??= incr(): 3 !== 2
Uncaught Error: obj[incr()] ??= incr(): 3 !== 2
The problem is that TypeScript evaluates the property key in the property access twice instead of capturing the value and only evaluating it once.
TypeScript generates this (incorrect):
(_a = (_b = obj)[incr()]) !== null && _a !== void 0 ? _a : (_b[incr()] = incr());
Babel generates this (correct):
(_a = obj[_b = incr()]) !== null && _a !== void 0 ? _a : obj[_b] = incr();
Playground Link: link
Related Issues: #37255 #37727
The text was updated successfully, but these errors were encountered:
Good catch. I'll take a look.
Sorry, something went wrong.
Successfully merging a pull request may close this issue.
TypeScript Version: Nightly
Search Terms: logical assignment side effect
Code
This code is from lgcl-nullish-assignment-operator-lhs-before-rhs.js, a test262 conformance test.
Expected behavior: This code should exit cleanly
Actual behavior: This code crashes with
Uncaught Error: obj[incr()] ??= incr(): 3 !== 2
The problem is that TypeScript evaluates the property key in the property access twice instead of capturing the value and only evaluating it once.
TypeScript generates this (incorrect):
Babel generates this (correct):
Playground Link: link
Related Issues: #37255 #37727
The text was updated successfully, but these errors were encountered: