Skip to content

Nested object rest in destructuring results in code with a syntax errorΒ #43400

@evanw

Description

@evanw

Bug Report

πŸ”Ž Search Terms

array object assign destructuring invalid code syntax error

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

var x, y, a, b;

// Should print: {"abc":1}
[{ ...x }] = [{ abc: 1 }]; console.log(JSON.stringify(x));

// Should print: {"abc":1}
for ([{ ...y }] of [[{ abc: 1 }]]) console.log(JSON.stringify(y));

// Should print: {"a":"a","b":{"1":"b","2":"c"}}
for ({ 0: a, ...b } in { abc: 1 }) console.log(JSON.stringify({a, b}));

πŸ™ Actual behavior

Compiling this with a target of ES2017 results in code with a syntax error:

"use strict";
var x, y, a, b;
// Should print: {"abc":1}
[Object.assign({}, x)] = [{ abc: 1 }];
console.log(JSON.stringify(x));
// Should print: {"abc":1}
for ([Object.assign({}, y)] of [[{ abc: 1 }]])
    console.log(JSON.stringify(y));
// Should print: {"a":"a","b":{"1":"b","2":"c"}}
for (Object.assign({ 0: a }, b) in { abc: 1 })
    console.log(JSON.stringify({ a, b }));

The specific syntax error is SyntaxError: Invalid destructuring assignment target, and happens because of the call to Object.assign in assignment position.

πŸ™‚ Expected behavior

This should result in valid code, and that code should print this:

{"abc":1}
{"abc":1}
{"a":"a","b":{"1":"b","2":"c"}}

I have reported this same issue in the past: #39181 (comment). It must be that either the fix has regressed or the fix was incomplete.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis 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