the tswhy logo: a question mark in a box

tswhy‽

A community effort to enrich TypeScript diagnostics.

Editing TS1091:

All diagnostics and fixes are authored in markdown. Propose any changes by editing the markdown. Additional fixes can be added. A preview of the rendered diagnostic will update when changes are made.

Once all proposed changes are made, the Propose button will submit the information and confirm raising the PR.

TS1091

Only a single variable declaration is allowed in a 'for...in' statement.

The for...in loop can be used to loop over all enumerable keys in an object. If you try to loop with multiple value variables this error will appear:

const list = [[1, 2], [3, 4]];
for (const a, b in list) {
  // ...
}
const obj = { a: 1, b: 2 };
for (const a, b in obj) {
  // ...
}

Fix: Use Object.entries to iterate over object keys and values.

To loop over the keys and values of an object, use Object.entries:

const obj = { a: 1, b: 2 };
for (const [key, val] of Object.entries(obj)) {
  /// ...
}

Fix: Use destructuring.

For other iterables, use destructuring:

const list = [[1, 2], [3, 4]];
for (const [a, b] of list) {
  // ...
}