Skip to content

Destructuring array polyfill isn't equivalent to new JS #32959

Closed
@Validark

Description

@Validark

Same deal as #31469

Search Terms: Destructuring array Polyfill concat

Code

const k = [1, 2];
const o = [3, ...k, k[0]++];
console.log(o.join(", "));

Compiles to

var k = [1, 2];
var o = [3].concat(k, [k[0]++]);
console.log(o.join(", "));

Or, on latest:

"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var k = [1, 2];
var o = __spreadArrays([3], k, [k[0]++]);
console.log(o.join(", "));

Expected behavior: 3, 1, 2, 1

Actual behavior: 3, 2, 2, 1

Playground Link: https://www.typescriptlang.org/play/index.html?target=1#code/MYewdgzgLgBA1jAvDA2gRgDQwEwF0DcAUKJLCEqgMxYB0dcWcKADLgNRsHHgQgA2AUxp8QAcwAUIGgCsQASzDiARFiUBKNfiA

Related Issues: #31469

The solution is the same as #32514. You can only concat/__spreadArrays one array at a time.

Also applies to call stacks:

let arr = [0];
console.log(...arr, arr[0]++);

Expected: 0, 0
Got: 1, 0

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions