objectAssignSpreads
Prefer object spread syntax over
Object.assign()when the first argument is an object literal.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
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 }.
Examples
Section titled “Examples”const clone = Object.assign({}, source);const merged = Object.assign({}, defaults, userConfig);const extended = Object.assign({ id: 1 }, data);const result = Object.assign({ name: "test" });const clone = { ...source };const merged = { ...defaults, ...userConfig };const extended = { id: 1, ...data };const result = { name: "test" };Object.assign(existingObject, newProperties);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useObjectSpread - ESLint:
prefer-object-spread - Oxlint:
eslint/prefer-object-spread