microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.07k stars 12.49k forks source link

Object Spread should not invoke setter. #13668

Open HerringtonDarkholme opened 7 years ago

HerringtonDarkholme commented 7 years ago

Current stage-3 spec requires object spread should define new properties on rest object, not set value on rest object.

Spec: http://sebmarkbage.github.io/ecmascript-rest-spread/#AbstractOperations-CopyDataProperties More readable example: https://github.com/sebmarkbage/ecmascript-rest-spread/blob/master/Issues.md#setters

TypeScript Version: nightly

Code

let record = { x: 1 };
let obj = { set x(a) { throw new Error(); }, ...record }; // not error
obj.x; // 1

Expected behavior: No runtime error.

Actual behavior: Runtime error.

Note: Babel does not define properties by default either.

RyanCavanaugh commented 7 years ago

Is this a curious issue of spec compliance, or a real-world thing?

HerringtonDarkholme commented 7 years ago

Purely compliance issue, I haven't seen any real world problem. Not even in Babel's issue list.

mgol commented 5 years ago

It's not just a question of spec compliance. First of all, Babel implements this behavior so people migrating from Babel to TypeScript may introduce bugs into their codebases. Secondly, this is important for some security-enhanced environments as it's possible to monkey-patch methods like Object.assign but not syntax like object spread. See the meeting notes of TC39 regarding this decision: https://tc39.github.io/tc39-notes/2014-09_sept-25.html#58-object-rest-destructuring-and-spread-properties

Andarist commented 2 months ago

@RyanCavanaugh based on some of your more recent comments I think you are not actively interested in issues like this. Perhaps this one can be closed?