• Error
  • Reported by the type checker

TS2488

Type '{0}' must have a '[Symbol.iterator]()' method that returns an iterator.

A value that isn't iterable is used with for...of, spread or array destructuring. Iterate over an array or iterable, or convert the value first.

What it means

for...of, [...x] and const [a, b] = x call the value's [Symbol.iterator]() method. When the type has no such method, the code would throw at runtime.

Example

index.ts
const counts = { a: 1, b: 2 };
for (const c of counts) {
  console.log(c);
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(2,17): error TS2488: Type '{ a: number; b: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Iterate over an iterable view: Object.values(counts), Object.entries(counts), map.entries().
  • Check the target lib includes the iterables you use (for example Map and Set iteration needs ES2015 or later).