Skip to content

Incorrect transpile for destructuring assignment of block-scoped loop variable in closure #7457

Description

@T18970237136

Hi,
it seems the fix for #6877 missed destructuring assignments.

TypeScript Version:
master (f06423b)

Code
Running the following code in tsc or in the playground:

(function() {
    "use strict";

    for (let i = 0; i < 4; i++) {
        (() => [i] = [i + 1])();
        console.log(i);
    }
})();

when targeting ES5 will be compiled to:

(function () {
    "use strict";
    var _loop_1 = function(i) {
        (function () { return (_a = [i + 1], i = _a[0], _a); var _a; })();
        console.log(i);
    };
    for (var i = 0; i < 4; i++) {
        _loop_1(i);
    }
})();

Actual behavior when running the compiled snipped:

1
2
3
4

Expected behavior (running the original snipped in Firefox or Chrome):

1
3

Expected compilation result:

(function () {
    "use strict";
    var _loop_1 = function(i) {
        (function () { return (_a = [i + 1], i = _a[0], _a); var _a; })();
        console.log(i);
        out_i_1 = i;
    };
    var out_i_1;
    for (var i = 0; i < 4; i++) {
        _loop_1(i);
        i = out_i_1;
    }
})();

Note that the same problem exists for object desctructuring.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions