Skip to content

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 and stylisticStrict 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.

declare const
const array: string[]
array
: string[];
const array: string[]
array
.
Array<string>.findIndex(predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any): number

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findIndex
((
item: string
item
) =>
item: string
item
=== "value");
declare const
const array: string[]
array
: string[];
const array: string[]
array
.
Array<string>.findIndex(predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any): number

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findIndex
((
item: string
item
) => "value" ===
item: string
item
);
declare const
const array: string[]
array
: string[];
const array: string[]
array
.findLastIndex((
item: any
item
) =>
item: any
item
=== "value");
Error ts(2550) ― Property 'findLastIndex' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
declare const
const array: string[]
array
: string[];
const array: string[]
array
.
Array<string>.findIndex(predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any): number

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

findIndex
(function (
item: string
item
) {
return
item: string
item
=== "value";
});

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.