src/content/docs/linter/rules/use-object-spread.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/style/useObjectSpread`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`prefer-object-spread`](https://eslint.org/docs/latest/rules/prefer-object-spread) - Inspired from [`e18e/prefer-spread-syntax`](https://github.com/e18e/eslint-plugin){
"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);
Object.assign({}, { foo: 'bar' });
Object.assign({ foo: 'bar' }, baz);
Object.assign({}, baz, { foo: 'bar' });
({ ...foo });
({ ...baz, foo: 'bar' });
Modifying an existing object is allowed:
Object.assign(foo, { bar: baz });