Skip to content

functionApplySpreads

Prefer the spread operator over .apply() calls.

✅ This rule is included in the ts stylistic preset.

Before ES2015, one had to use Function.prototype.apply() to call variadic functions with an array of arguments. In ES2015 and later, the spread operator (...) provides cleaner syntax for the same purpose.

This rule flags usage of .apply() when it can be replaced with spread syntax. It only reports cases where the this argument is not changed, meaning the spread replacement is safe.

foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
Math.max.apply(Math, numbers);

This rule is not configurable.

This rule should not be used if you need to target ES5 or earlier JavaScript environments that don’t support the spread operator.

If you have code that intentionally uses .apply() for readability or other reasons, you may want to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.