Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Rule proposal: Preferring .find(...) over .filter(...)[0] #6886

Copy link
Copy link
Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg
Issue body actions

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

I've seen occasional code authors write code with a .filter returning element [0], perhaps not knowing that .find does the same thing. Perhaps we could write a rule that flags for this, and uses the type system to know whether the collection being .filtered is an array/array-like?

Fail Cases

const values = ['a', 'b ', 'c'];

const result = values.filter(value => value.includes(' '))[0];

Pass Cases

const values = ['a', 'b ', 'c'];

const result = values.find(value => value.includes(' '));

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.