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.
Examples
Section titled “Examples”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;const values = [1, 2, 3];const hasPositive = values.some((value) => value > 0);const items = ["a", "b", "c"];const hasMatch = items.some((item) => item === "b");const values = [1, 2, 3];const filtered = values.filter((value) => value > 0);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-array-some - Oxlint:
unicorn/prefer-array-some
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.