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  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

β„Ή Use object spread instead of Object.assign.

> 1 β”‚ Object.assign({}, foo);
β”‚ ^^^^^^^^^^^^^
2 β”‚

β„Ή Object spread syntax is more concise, readable, and performs better than Object.assign.

β„Ή 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  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

β„Ή Use object spread instead of Object.assign.

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

β„Ή Object spread syntax is more concise, readable, and performs better than Object.assign.

β„Ή 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  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

β„Ή Use object spread instead of Object.assign.

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

β„Ή Object spread syntax is more concise, readable, and performs better than Object.assign.

β„Ή 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  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

β„Ή Use object spread instead of Object.assign.

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

β„Ή Object spread syntax is more concise, readable, and performs better than Object.assign.

β„Ή 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 });