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 const clone: any
clone = var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<{}, any>(target: {}, source: any): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign({}, const source: any
source);const const merged: any
merged = var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<{}, any, any>(target: {}, source1: any, source2: any): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign({}, const defaults: any
defaults, const userConfig: any
userConfig);const const extended: any
extended = var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<{ id: number;}, any>(target: { id: number;}, source: any): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign({ id: number
id: 1 }, const data: any
data);const const result: any
result = var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign(target: object, ...sources: any[]): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign({ name: string
name: "test" });const const clone: any
clone = { ...const source: any
source };const const merged: any
merged = { ...const defaults: any
defaults, ...const userConfig: any
userConfig };const const extended: any
extended = { id: number
id: 1, ...const data: any
data };const const result: { name: string;}
result = { name: string
name: "test" };var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.assign<any, any>(target: any, source: any): any (+3 overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
assign(const existingObject: any
existingObject, const newProperties: any
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