New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule proposal: Ban 'in' operator for arrays #5474
Comments
SGTM! As stated & with |
I'd like to work on this issue! though it will take me some time. |
✅ Just needs a review |
@JoshuaKGoldberg i wonder if we should merge this with #5677 It seems like they're doing the same thing, except #5677 covers more cases. |
I agree, but i think it should be too hard. |
@mahdi-farnia by using types! All of the relevant information is available on the types. You can play around in https://ts-ast-viewer.com to get a feel for how different cases might look. Grep our codebase fir Your code is a good special case message to start with - but we should do more than just arrays if we can! |
@bradzacher I've done exploring and working with astexplorer. But my question remains: which types are indexable? |
Any type with an index signature is indexable. Eg type T = { [k: string]: number} But we should still ensure we disallow string likes and arrays |
@bradzacher As i said everything in JavaScript is a pair of keys and values:
|
You can use the type checker for this. Just playing around on my end, I do see an |
Agreed, good point. Closing this as a dup. Thanks for the discussion all! |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Checking for array membership using
in
is a common new JS developer mistake - it looks like it should work but it does not sincein
checks for properties, and array values are not properties.Fail Cases
Pass Cases
Additional Info
There is a very similar https://typescript-eslint.io/rules/no-for-in-array rule for
for-in
loops specificallyThe text was updated successfully, but these errors were encountered: