This rule raises an issue when the spread operator (…) is used in cases where it provides no benefit, such as spreading array
literals directly into arrays, spreading object literals into objects, or converting iterables to arrays when the target already accepts
iterables.
The spread operator (…) is a powerful JavaScript feature for expanding arrays and objects. However, using it unnecessarily can make
code harder to read and potentially less efficient.
There are several common patterns where spread syntax adds no value:
[item] directly into another array or function call, you can
simply write the item directly. Set, Map, Promise.all(), and
Array.from() accept any iterable object, not just arrays. Converting an iterable to an array first using spread syntax is unnecessary.
for…of loops and yield* expressions work directly with
iterables, so spreading them into arrays first is redundant. These unnecessary uses of spread syntax can:
Using unnecessary spread syntax makes code less readable and may create performance overhead by creating intermediate arrays when they’re not needed. While the performance impact is typically minimal, the readability impact can be significant, especially in codebases with many such occurrences.
Remove unnecessary spread syntax when spreading array literals directly into arrays or function calls.
const array = [firstElement, ...[secondElement], thirdElement]; // Noncompliant foo(firstArgument, ...[secondArgument], thirdArgument); // Noncompliant
const array = [firstElement, secondElement, thirdElement]; foo(firstArgument, secondArgument, thirdArgument);