Suggestion: disallow for-in with Array types #9645
Labels
Out of Scope
This idea sits outside of the TypeScript language design constraints
Suggestion
An idea for TypeScript
Here's a function I just wrote:
It type checks fine, but it has a bug! The
for .. in
should really befor .. of
.If a variable is known to have an array type, then using
for .. in
to iterate over it is a very strange thing to do. It will iterate over the indices, converting them to strings, and skip over any holes in the array.I'd propose that
for .. in
with an Array be an error. You probably wantfor .. of
. Orarray.forEach
. If you really wantfor .. in
for some reason, you could always useObject.keys(array).forEach
.This might be better implemented as a lint rule. See palantir/tslint#1380
The text was updated successfully, but these errors were encountered: