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

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
const values: number[]
values
= [1, 2, 3];
const
const hasPositive: boolean
hasPositive
=
const values: number[]
values
.
Array<number>.filter(predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
((
value: number
value
) =>
value: number
value
> 0).
Array<number>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
> 0;
const
const items: string[]
items
= ["a", "b", "c"];
const
const hasMatch: boolean
hasMatch
=
const items: string[]
items
.
Array<string>.filter<"b">(predicate: (value: string, index: number, array: string[]) => value is "b", thisArg?: any): "b"[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
((
item: string
item
) =>
item: string
item
=== "b").
Array<"b">.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
!== 0;
const
const values: number[]
values
= [1, 2, 3];
const
const hasMatch: boolean
hasMatch
=
const values: number[]
values
.
Array<number>.findIndex(predicate: (value: number, index: number, obj: number[]) => 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
((
value: number
value
) =>
value: number
value
> 2) !== -1;
const
const items: string[]
items
= ["a", "b", "c"];
const
const exists: boolean
exists
=
const items: string[]
items
.findLastIndex((
item: any
item
) =>
item: any
item
=== "a") !== -1;
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.

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 ❤️‍🔥 around the world by the Flint team and contributors.