Skip to content

useObjectSpread (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"style": {
"useObjectSpread": "error"
}
}
}
}

Prefer object spread over Object.assign() when constructing new objects.

Object spread syntax is more concise, more readable, and performs better than Object.assign() when creating a new object from existing objects. It also has better TypeScript integration.

Object.assign({}, foo);
code-block.js:1:1 lint/style/useObjectSpread  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Object spread syntax is more concise, readable, and performs better than Object.assign.

> 1 │ Object.assign({}, foo);
│ ^^^^^^^^^^^^^
2 │

ℹ Safe fix: Replace Object.assign({...}, <object>) with { ...<object> }.

1 │ - Object.assign({},·foo);
1 │ + ({...foo,});
2 2 │

Object.assign({}, { foo: 'bar' });
code-block.js:1:1 lint/style/useObjectSpread  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Object spread syntax is more concise, readable, and performs better than Object.assign.

> 1 │ Object.assign({}, { foo: 'bar' });
│ ^^^^^^^^^^^^^
2 │

ℹ Safe fix: Replace Object.assign({...}, <object>) with { ...<object> }.

1 │ - Object.assign({},·{·foo:·'bar'·});
1 │ + ({foo:·'bar'·,});
2 2 │

Object.assign({ foo: 'bar' }, baz);
code-block.js:1:1 lint/style/useObjectSpread  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Object spread syntax is more concise, readable, and performs better than Object.assign.

> 1 │ Object.assign({ foo: 'bar' }, baz);
│ ^^^^^^^^^^^^^
2 │

ℹ Safe fix: Replace Object.assign({...}, <object>) with { ...<object> }.

1 │ - Object.assign({·foo:·'bar'·},·baz);
1 │ + ({foo:·'bar'·,...baz,});
2 2 │

Object.assign({}, baz, { foo: 'bar' });
code-block.js:1:1 lint/style/useObjectSpread  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Object spread syntax is more concise, readable, and performs better than Object.assign.

> 1 │ Object.assign({}, baz, { foo: 'bar' });
│ ^^^^^^^^^^^^^
2 │

ℹ Safe fix: Replace Object.assign({...}, <object>) with { ...<object> }.

1 │ - Object.assign({},·baz,·{·foo:·'bar'·});
1 │ + ({...baz,foo:·'bar'·,});
2 2 │

({ ...foo });
({ ...baz, foo: 'bar' });

Modifying an existing object is allowed:

Object.assign(foo, { bar: baz });