Skip to content

typescript/prefer-for-of Style ​

🚧 An auto-fix is still under development.

What it does ​

Enforces the use of for-of loop instead of a for loop with a simple iteration.

Why is this bad? ​

Using a for loop with a simple iteration over an array can be replaced with a more concise and readable for-of loop. For-of loops are easier to read and less error-prone, as they eliminate the need for an index variable and manual array access.

Example ​

Examples of incorrect code for this rule:

typescript
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

Examples of correct code for this rule:

typescript
for (const item of arr) {
  console.log(item);
}

References ​

Released under the MIT License.