Skip to content

Syntax error when lowering object rest into Object.assign #39181

@evanw

Description

@evanw

TypeScript Version: Nightly

Search Terms: object rest property binding Object.assign

Code

var y
({ x: { ...y } } = { x: { z: 1 } })
console.log(y.z === 1) // true

Expected behavior:

The code above should print true without crashing.

Actual behavior:

The TypeScript compiler generates this code when targeting ES2017 or below, which is invalid JavaScript:

var y;
({ x: Object.assign({}, y) } = { x: { z: 1 } });
console.log(y.z === 1); // true

Attempting to run this code gives Uncaught SyntaxError: Invalid destructuring assignment target in V8.

Babel roughly generates this code, which works correctly:

var y;
var _x = { x: { z: 1 } };
y = Object.assign({}, _x.x);
console.log(y.z === 1); // true

This came up because I'm trying to implement the rest/spread proposal for esbuild by following what the TypeScript compiler does. I logged this issue when I discovered the TypeScript compiler isn't a correct implementation.

Playground Link: link

Related Issues:

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