arrayIndexOfMethods
Reports using
.findIndex()or.findLastIndex()with simple equality checks that can be replaced with.indexOf()or.lastIndexOf().
✅ This rule is included in the ts stylistic presets.
Array.prototype.findIndex() and Array.prototype.findLastIndex() are intended for more complex predicate checks.
When the callback only performs a simple strict equality check (x === value), .indexOf() or .lastIndexOf() are more readable and expressive.
This rule reports when .findIndex() or .findLastIndex() can be simplified.
Examples
Section titled “Examples”declare const array: string[];array.findIndex((item) => item === "value");declare const array: string[];array.findIndex((item) => "value" === item);declare const array: string[];array.findLastIndex((item) => item === "value");declare const array: string[];array.findIndex(function (item) { return item === "value";});declare const array: string[];array.indexOf("value");declare const array: string[];array.lastIndexOf("value");declare const array: string[];array.findIndex((item) => item.startsWith("v"));declare const array: number[];array.findIndex((item) => item > 0);declare const array: object[];array.findIndex((item) => item.id === 1);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer the functional style of .findIndex() for consistency, or if your codebase has a large number of existing uses, you may disable this rule.
Further Reading
Section titled “Further Reading”- MDN: Array.prototype.indexOf()
- MDN: Array.prototype.lastIndexOf()
- MDN: Array.prototype.findIndex()
- MDN: Array.prototype.findLastIndex()
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useIndexOf - ESLint:
unicorn/prefer-array-index-of - Oxlint:
unicorn/prefer-array-index-of
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.