Description
I was wondering in what order destructuring is supposed to execute expressions, so I wrote the following test. I tried it in Chrome's V8 and Node, and they both gave me the same answer. I tried it in ts-node/TS, and it gave me a different answer.
TypeScript Version: any
Search Terms: destructuring breaks program order
Code
function f() {
console.log("f");
return { x: 1 };
}
function g() {
console.log("g");
return "x" as "x";
}
const { [g()]: x } = f();
Expected behavior: f g
Actual behavior: g f
Code 2
This also means that the program state can be contaminated (with bad practices?)
let i = 0;
function f() {
console.log(i);
return { 0: "a", 1: "b", 2: "c" } as { [key: number]: string };
}
const { [++i]: x } = f();
Expected behavior: 0
Actual behavior: 1
Related Issues: #31469
I don't understand what ECMAScript is saying about what it should be, but I believe this is relevant: https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation
In my opinion, TS's compilation makes the most sense. I don't know who is correct. Maybe TS is correct and everyone else is wrong. Again, I don't understand the ECMAScript document. But I can definitively say that TS differs from everyone else in this way.