🔍 requires type information 🔧 fixable
Prefer object spread over Object.assign
for copying properties to a new object.
ECMAScript 2018 added a way to syntactically express shallow copying of object properties. Before you would typically use Object.assign
or a handwritten alternative.
TypeScript can check calls to Object.assign
only for a limited number of arguments and cannot infer the correct return value. For object spread on the other hand it can infer (almost) the correct type and even check for duplicate properties.
Object spread provides additional safety as its behavior cannot be overridden, which is possible for Object.assign
. Furthermore it avoids unintentionally modifying the first argument of Object.assign
caused by some sort of refactoring.
Some JavaScript VMs may be able to further optimize object spread.
In addition the rule no-duplicate-spread-property
provides additional checks to avoid duplicate properties or object spread whose properties are overridden by later properties or spread.
👎 Examples of incorrect code
declare const someObj: Record<string, string>;
Object.assign({}, someObj);
Object.assign({}, {prop: 1});
Object.assign({prop: 1}, someObj);
👍 Examples of correct code
declare const someObj: Record<string, string>;
// fixed examples from above
({...someObj});
({prop: 1});
({prop: 1, ...someObj});
declare const someArr: Array<typeof someObj>;
Object.assign({}, ...someArr); // array spread cannot be expressed in object spread
Object.assign(someObj, {prop: 1}); // modifies the first argument
function copy<T>(obj: T) {
return Object.assign({}, obj); // TypeScript currently doesn't support spreading type parameters
}