• 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

index.ts
const total = 3;
for (const key in total) {
  console.log(key);
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.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...of for the values of an array or the characters of a string.
  • Use a counting loop for a number of iterations.