Explicit any type in for..in loop
See original GitHub issueFor 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:
- Created 8 years ago
- Reactions:22
- Comments:41 (16 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In a for…in loop
I really think that
x
should be implicitly typed tokeyof y
instead ofstring
(orany
) considering that the loop will only assign enumerable non-Symbol properties of they
and nothing else can be assigned tox
as it’s aconst
.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.[ts] Element implicitly has an ‘any’ type because type ‘{ atk: number; def: number; }’ has no index signature.
[ts] The left-hand side of a ‘for…in’ statement cannot use a type annotation.
[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
which works, but involves an unsafe cast. Is there a way to do this safely?