# @typescript-eslint/prefer-find

> Require `.find()` instead of `.filter()[0]` to get the first match

`.filter(...)[0]` tests every element and builds a new array, then uses only its first element. `.find(...)` stops at the first match and returns it.

`.filter(...).at(0)` is reported too. Only arrays and tuples are checked.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-find" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
declare const users: { name: string; admin: boolean }[];

const admin = users.filter(user => user.admin)[0];
const guest = users.filter(user => !user.admin).at(0);
```

Not reported:

{/* good */}
```ts
declare const users: { name: string; admin: boolean }[];

const admin = users.find(user => user.admin);
const second = users.filter(user => user.admin)[1];
```

A `filter` method on your own class is left alone:

{/* good */}
```ts
class Query {
  filter(condition: string): string[] {
    return [condition];
  }
}

const row = new Query().filter("active = 1")[0];
```
