oxc-project/oxc · error · OxcDiagnostic
Expected a `for...of` loop instead of a `for` loop with this
Error message
Expected a `for...of` loop instead of a `for` loop with this simple iteration.
What it means
Raised by typescript/prefer_for_of when a `for` loop is a simple index iteration over an array (counter starts at 0, condition is index < arr.length, increment by one, and the body only uses `arr[i]` without reassigning the index or mutating the array's length). At the throw site the loop can be expressed more safely and readably as `for (const item of arr)`, eliminating off-by-one hazards. prefer_for_of_diagnostic fires from run() after the structural checks confirm the pattern is a plain iteration.
Source
Thrown at crates/oxc_linter/src/rules/typescript/prefer_for_of.rs:16
use oxc_ast::{
AstKind,
ast::{
AssignmentTarget, BindingPattern, Expression, ForStatementInit, SimpleAssignmentTarget,
VariableDeclarationKind, match_member_expression,
},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator, UpdateOperator};
use crate::{AstNode, context::LintContext, rule::Rule, utils::is_same_expression};
fn prefer_for_of_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Expected a `for...of` loop instead of a `for` loop with this simple iteration.",
)
.with_help("Consider using a `for...of` loop for this simple iteration.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferForOf;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces the use of a `for...of` loop instead of a `for` loop with 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 theyView on GitHub (pinned to e1e7af627c)
Solutions
- Rewrite the loop as for (const item of array) { ... }.
- If array methods fit, use forEach/map instead.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/typescript/prefer_for_of.rs:16 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a1d2c095b2c276a2.
Report an issue: GitHub.