prefer-spread
Configuration
rslint.config.ts
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
thisbinding of the applied function:- When the function is not a member expression, only
null/undefined/void 0pass (otherwise thethisbinding 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.objon both sides).
- When the function is not a member expression, only
Examples of incorrect code for this rule:
Examples of correct code for this rule: