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
const clone: Record<string, unknown>
clone
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.assign<{}, Record<string, unknown>>(target: {}, source: Record<string, unknown>): Record<string, unknown> (+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.

@paramtarget The target object to copy to.

@paramsource The source object from which to copy properties.

assign
({},
const source: Record<string, unknown>
source
);
const
const merged: Record<string, unknown>
merged
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.assign<{}, Record<string, unknown>, Record<string, unknown>>(target: {}, source1: Record<string, unknown>, source2: Record<string, unknown>): Record<string, unknown> (+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.

@paramtarget The target object to copy to.

@paramsource1 The first source object from which to copy properties.

@paramsource2 The second source object from which to copy properties.

assign
({},
const defaults: Record<string, unknown>
defaults
,
const userConfig: Record<string, unknown>
userConfig
);
const
const extended: {
id: number;
} & Record<string, unknown>
extended
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.assign<{
id: number;
}, Record<string, unknown>>(target: {
id: number;
}, source: Record<string, unknown>): {
id: number;
} & Record<string, unknown> (+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.

@paramtarget The target object to copy to.

@paramsource The source object from which to copy properties.

assign
({
id: number
id
: 1 },
const data: Record<string, unknown>
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.

@paramtarget The target object to copy to.

@paramsources One or more source objects from which to copy properties

assign
({
name: string
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 ❤️‍🔥 around the world by the Flint team and contributors.