@typescript-eslint/prefer-for-of

Require for...of instead of a for loop that only uses its index to read the array

Also called typescript-eslint/prefer-for-of, typescript/prefer-for-of

A for loop with a counter has three places you can get wrong: the start, the end, and the step. If you only use the counter to read items[i], a for...of loop gives you the element directly and has none of them.

A loop is reported if it starts at 0, runs while i < items.length, goes up by one, and uses i in its body only to read items[i].

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-for-of" = "error" # or "warn"

Examples#

example.ts1 problem
for (let i = 0; i < items.length; i++) {
  console.log(items[i]);
}
example.tsNo problems
for (const item of items) {
  console.log(item);
}

The loop is left alone when it needs the index for something else, or when it writes to the array:

example.tsNo problems
for (let i = 0; i < items.length; i++) {
  console.log(i, items[i]);
}

for (let i = 0; i < items.length; i++) {
  items[i] = 0;
}

for (let i = 0; i < items.length; i++) {
  console.log(items[i], others[i]);
}