Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 718 Bytes

no-mutating-assign.md

File metadata and controls

23 lines (17 loc) · 718 Bytes

Forbid the use of Object.assign() with a variable as first argument

Object.assign() is a method that mutates its first argument. In order to use this method as a non-mutating method, the first element may not be a variable (even if declared using const), and should therefore be a static value, such as an object expression.

Fail

var a = {foo: 1, bar: 2};
var b = {bar: 3};
Object.assign(a, b);

Pass

var a = {foo: 1, bar: 2};
var b = {bar: 3};
Object.assign({}, a, b);
Object.assign({foo: 1, bar: 2}, b);
Object.assign(function foo() {}, {propTypes: {}});