π Prefer direct iteration over default iterator method calls.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Some built-ins expose their default iterator through both [Symbol.iterator]() and a named method. For example, Map#entries() is the same iterator as Map#[Symbol.iterator](), and Array#values() is the same iterator as Array#[Symbol.iterator]().
When the result is immediately consumed as an iterable, the named method is redundant.
This rule reports:
.entries()forMap,FormData, andURLSearchParams.values()forArray, typed arrays, andSet.keys()forSet
It intentionally does not report unknown receivers, or methods that are not equivalent to the default iterator, such as Array#entries(), Map#values(), or Set#entries().
With TypeScript type information, this rule can also recognize supported built-ins through type aliases and inferred references. Without type information, it only uses direct syntax and simple annotations.
// β
const map = new Map();
for (const entry of map.entries()) {}// β
const map = new Map();
for (const entry of map) {}// β
const array = [1, 2, 3];
const values = [...array.values()];// β
const array = [1, 2, 3];
const values = [...array];// β
const map = new Map();
const copy = new Map(map.entries());// β
const map = new Map();
const copy = new Map(map);// β
β `Array#entries()` yields `[index, value]` pairs, while arrays iterate values by default
for (const entry of array.entries()) {}// β
β unknown receiver
for (const item of collection.values()) {}