> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# prefer-spread

[Added in v0.5.0](https://github.com/web-infra-dev/rslint/releases/tag/v0.5.0)
## Configuration

| Preset                              | Configured Value |
| ----------------------------------- | ---------------- |
| ✅ ts.configs.recommended            | `"error"`        |
| ✅ ts.configs.recommendedTypeChecked | `"error"`        |
| ✅ ts.configs.strict                 | `"error"`        |
| ✅ ts.configs.strictTypeChecked      | `"error"`        |
| ✅ ts.configs.stylistic              | `"error"`        |
| ✅ ts.configs.stylisticTypeChecked   | `"error"`        |


```ts title=rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'prefer-spread': 'error',
    },
  },
]);
```

## Rule Details

Suggest using the spread operator instead of `.apply()`.

Before ES2015, `Function.prototype.apply()` was the only way to call a variadic
function with an array of arguments. With the spread operator (`...`),
`function(...args)` achieves the same effect more concisely and also works in
`new` expressions, which `.apply()` does not support.

This rule flags `.apply()` calls that are interchangeable with a spread call:

- The second argument of `.apply()` is neither an array literal nor a spread
  element (those forms already behave like a spread call).
- The first argument preserves the `this` binding of the applied function:
  - When the function is not a member expression, only `null` / `undefined` /
    `void 0` pass (otherwise the `this` binding may change on migration).
  - When the function is a member expression (e.g. `obj.foo.apply(obj, args)`),
    the first argument must produce the same token stream as the member's
    object (e.g. `obj` on both sides).

Examples of **incorrect** code for this rule:

```javascript
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
```

Examples of **correct** code for this rule:

```javascript
// The `this` binding is changed deliberately
foo.apply(obj, args);
obj.foo.apply(null, args);
obj.foo.apply(otherObj, args);

// The second argument is not variadic
foo.apply(undefined, [1, 2, 3]);
obj.foo.apply(obj, [1, 2, 3]);

// Already using a spread call
obj.foo(...args);
```

## Original Documentation

- [ESLint: prefer-spread](https://eslint.org/docs/latest/rules/prefer-spread)
- [Source code](https://github.com/eslint/eslint/blob/v10.8.1/lib/rules/prefer-spread.js)
