Skip to content

Rule proposal: Ban 'in' operator for arrays  #5474

@ZhangYiJiang

Description

@ZhangYiJiang

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Checking for array membership using in is a common new JS developer mistake - it looks like it should work but it does not since in checks for properties, and array values are not properties.

Fail Cases

const arr = ['a', 'b', 'c'];
const inArray = 'a' in arr;

Pass Cases

const arr = ['a', 'b', 'c'];
const inArray = arr.includes('a');

Additional Info

There is a very similar https://typescript-eslint.io/rules/no-for-in-array rule for for-in loops specifically

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions