useObjectSpread (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v2.0.0 - Diagnostic Category:
lint/style/useObjectSpread - This rule isnβt recommended, so you need to enable it.
- This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Same as
prefer-object-spread - Inspired from
e18e/prefer-spread-syntax
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "style": { "useObjectSpread": "error" } } }}Description
Section titled βDescriptionβ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.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβ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 });Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.