question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicit any type in for..in loop

See original GitHub issue

For the reason of consistency, it should be possible to explicitly annotate the iterator variable at least with the “any” type in a for…in loop.

Edit from @sandersn: Fixes should apply to JSDoc too: https://github.com/microsoft/TypeScript/issues/43756, and perhaps allow annotations on for .. of too

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:22
  • Comments:41 (16 by maintainers)

github_iconTop GitHub Comments

64reactions
NemoSteincommented, Feb 9, 2021

In a for…in loop

for (const x in y) { ... }

I really think that x should be implicitly typed to keyof y instead of string (or any) considering that the loop will only assign enumerable non-Symbol properties of the y and nothing else can be assigned to x as it’s a const.

25reactions
Zarelcommented, May 14, 2018

I’m not sure if this is the right place to talk about this, but I have no idea how I’m supposed to safely use for..in in TypeScript.

type Stats = {atk: number, def: number};

function clearStats(stats: Stats) {
  for (var stat in stats) stats[stat] = 0;
}

[ts] Element implicitly has an ‘any’ type because type ‘{ atk: number; def: number; }’ has no index signature.

type StatName = 'atk' | 'def';
type Stats = {atk: number, def: number};

function clearStats(stats: Stats) {
  for (var stat: StatName in stats) stats[stat] = 0;
}

[ts] The left-hand side of a ‘for…in’ statement cannot use a type annotation.

type StatName = 'atk' | 'def';
type Stats = {[stat: StatName]: number};

function clearStats(stats: Stats) {
  for (var stat in stats) stats[stat] = 0;
}

[ts] An index signature parameter type must be ‘string’ or ‘number’.

Is it just me, or should there be a way to strongly type this?

I’ve been using

type StatName = 'atk' | 'def';
type Stats = {atk: number, def: number};

function clearStats(stats: Stats) {
  for (var stat in stats) stats[stat as StatName] = 0;
}

which works, but involves an unsafe cast. Is there a way to do this safely?

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - for-in statement - Stack Overflow
The for-in statement is really there to enumerate over object properties, which is how it is implemented in TypeScript. There are some issues ......
Read more >
ESLint Plugin TypeScript - npm
Name ✔️ 🔧 @typescript‑eslint/adjacent‑overload‑signatures ✔️ @typescript‑eslint/array‑type ✔️ 🔧 @typescript‑eslint/ban‑types ✔️ 🔧
Read more >
no-for-in-array - typescript-eslint
A for-in loop ( for (var i in o) ) iterates over the properties of an Object. While it is legal to use...
Read more >
FOR LOOP Statement
Name for the implicitly declared integer variable that is local to the FOR LOOP statement. Statements outside the loop cannot reference index ....
Read more >
Python "for" Loops (Definite Iteration)
Each time through the loop, the variable i takes on the value of the next object in <collection> . This type of for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found