Skip to content

objectAssignSpreads

Prefer object spread syntax over Object.assign() when the first argument is an object literal.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Object spread syntax ({ ...source }) provides a more concise and readable way to copy and merge objects compared to Object.assign(). When Object.assign() is called with an object literal as its first argument, the same result can be achieved more idiomatically using spread syntax.

This rule flags cases where Object.assign({}, source) or Object.assign({ prop: value }, source) can be replaced with { ...source } or { prop: value, ...source }.

const clone = Object.assign({}, source);
const merged = Object.assign({}, defaults, userConfig);
const extended = Object.assign({ id: 1 }, data);
const result = Object.assign({ name: "test" });

This rule is not configurable.

If your codebase needs to support older JavaScript environments (pre-ES2018) that don’t have native spread syntax support, you may need to disable this rule or use a transpiler. Alternately, some projects may prefer to use Object.assign() for its mutability, or may use it in many places where spread syntax is not appropriate and prefer always using it for consistency.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.