- 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
const counts = { a: 1, b: 2 };
for (const c of counts) {
console.log(c);
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.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
libincludes the iterables you use (for exampleMapandSetiteration needs ES2015 or later).