Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add a rule to disallow "for … in" on array types #1380

Closed
danvk opened this issue Jul 7, 2016 · 2 comments
Closed

Add a rule to disallow "for … in" on array types #1380

danvk opened this issue Jul 7, 2016 · 2 comments

Comments

@danvk
Copy link
Contributor

danvk commented Jul 7, 2016

This is almost certainly an error:

function foo(values: number[]) {
  for (const value in values) {
    ...
  }
}

It should be "for … of". If the intention is to iterate over the array indices, that's better done with values.forEach or a traditional for (let i = 0; i < values.length; i++) loop. Both of these alternatives give you numeric indices, rather than the strings that "for ... in" does.

Some people advocate disallowing for-in entirely, but I still find it useful for objects.

In theory tslint should know whether the object being iterated over is really an array and it could warn specifically on this mistake.

@adidahiya
Copy link
Contributor

This requires type checker information, which should be coming soon (#1363)!

@danvk
Copy link
Contributor Author

danvk commented Jul 12, 2016

I've sent out a PR to implement this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants