Skip to content
← Back to rules

typescript/prefer-find Nursery

💭 This rule requires type information.

何をするか

単一の要素を取得する場合、.filter(...)[0] の代わりに .find(...) を使用することを推奨します。

なぜ問題か

.filter(...)[0] は中間配列を作成し、意図が明確ではありません。 .find(...) は、最初の一致する要素のみが必要であることを直接表現しています。

このルールに対して誤りなコードの例:

ts
const first = list.filter((item) => item.active)[0];

このルールに対して正しいコードの例:

ts
const first = list.find((item) => item.active);

使用方法

設定ファイルまたは CLI でこのルールを有効化するには、以下のようにします:

json
{
  "rules": {
    "typescript/prefer-find": "error"
  }
}
bash
oxlint --type-aware --deny typescript/prefer-find

参照