You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
I was converting some code from using plain objects as maps and sets to using the ES6 Map and Set classes. TypeScript handily caught all of the places where the code did things like map[key] = value and delete map[key], but it did not catch uses of key in map and for (const key in map) .... This seems like something tslint would be very useful for.
While just applying the rule to Map and Set would be nice, it might be possible to generalize it, either to any iterable type, or to any type that isn't any or indexable.
TSLint version: 4.5.1
TypeScript version: 2.2.1
Running TSLint via: CLI / VSCode
TypeScript code to lint
constmap=newMap<string,string>();for(constkeyinmap){// <-- should use `const [key, value] of map` insteadif(map.hasOwnProperty(key)){// <-- maybe also warn about this/* ... */}}if("thing"inmap){// <-- should use `map.has("thing")` instead/* ... */}constset=newSet<string>();for(constkeyinset){// <-- should use `const key of set` insteadif(set.hasOwnProperty(key)){// <-- maybe also warn about this/* ... */}}if("thing"inset){// <-- should use `set.has("thing")` instead/* ... */}
The text was updated successfully, but these errors were encountered:
I'm having a little trouble coming up with an implementation. At one of those lines, where I think the for-in expression should have type Map, the Type obtained has no symbol, flags is 1 ("any"). I'm not sure how to find out that the type is Map, or better yet whether the type has an implementation for the Symbol.iterator property.
Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you really need the rule, custom rules are always an option and can be maintained outside this repo!
Rule Request
I was converting some code from using plain objects as maps and sets to using the ES6
Map
andSet
classes. TypeScript handily caught all of the places where the code did things likemap[key] = value
anddelete map[key]
, but it did not catch uses ofkey in map
andfor (const key in map) ...
. This seems like something tslint would be very useful for.While just applying the rule to Map and Set would be nice, it might be possible to generalize it, either to any iterable type, or to any type that isn't
any
or indexable.TypeScript code to lint
The text was updated successfully, but these errors were encountered: