Skip to content

arraySomeMethods

Reports patterns that can be replaced with .some() for checking array element existence.

✅ This rule is included in the ts stylistic preset.

Using .some() is the clearest and most efficient way to check if an array contains an element matching a condition. Patterns like .filter(...).length > 0 or .findIndex(...) !== -1 are less readable and potentially less efficient.

const values = [1, 2, 3];
const hasPositive = values.filter((value) => value > 0).length > 0;
const items = ["a", "b", "c"];
const hasMatch = items.filter((item) => item === "b").length !== 0;
const values = [1, 2, 3];
const hasMatch = values.findIndex((value) => value > 2) !== -1;
const items = ["a", "b", "c"];
const exists = items.findLastIndex((item) => item === "a") !== -1;

This rule is not configurable.

If you need the filtered array for other purposes beyond checking existence, or if you’re working in a codebase that prefers explicit length checks for consistency, you may want to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.