# prefer-object-spread

> Require object spread instead of `Object.assign()` into a new object

`Object.assign({}, defaults, options)` builds a new object, but you have to notice the `{}` to know that nothing is changed in place. `{ ...defaults, ...options }` is shorter and can only make a new object.

A call is only reported if its first argument is an object literal. `Object.assign(target, options)` changes `target`, which spread can't do. A call is also left alone if it spreads its arguments, or if one of its object literals has a getter or a setter.

`--fix` rewrites the call as an object literal.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"prefer-object-spread" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
const settings = Object.assign({}, defaults, options);
const admin = Object.assign({ role: "admin" }, user);
const point = Object.assign({ x: 0, y: 0 });
```

Not reported:

{/* good */}
```js
const settings = { ...defaults, ...options };
const admin = { role: "admin", ...user };
const point = { x: 0, y: 0 };

Object.assign(target, options);
const merged = Object.assign({}, ...sources);
const list = Object.assign({ get size() { return items.length; } }, base);
```
