- Error
- Reported by the type checker
TS2407
The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type '{0}'.
The value after "in" in a for...in loop is a primitive. Loop over an object, or use for...of for strings and arrays.
What it means
for...in walks the enumerable keys of an object. Using it on a value whose type is a primitive such as number or boolean is reported, because it has no keys of its own to visit.
Example
const total = 3;
for (const key in total) {
console.log(key);
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,19): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type '3'.
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
- Loop over the object you meant:
for (const key in record). - Use
for...offor the values of an array or the characters of a string. - Use a counting loop for a number of iterations.