Spread syntax can be used when all elements from an object or array need to be included in a list of some kind.
(c) MDN
🐊Putout plugin adds ability to remove useless spread syntax.
npm i @putout/plugin-remove-useless-spread
{
"rules": {
"remove-useless-spread/array": "on",
"remove-useless-spread/object": "on"
}
}
The thing is [...b]
can be used for:
- copying an array;
- converting different value type like
string
to anarray
.
So better to be more concrete in and use slice
for copying and Array()
for converting to decrease cognitive load.
Also sometimes there is no need on any of this operations, and we can drop spread
.
for (const a of [...b]) {}
const places = [...getPlaces()];
for (const a of b) {}
const places = getPlaces();
// Array constructor creates sparse array
[...Array(5)].map(Number);
const a = {
...fn(),
};
const a = fn();
MIT