Skip to content

arrayIncludes

Reports using .indexOf() comparisons that can be replaced with .includes().

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Prior to ES2015, Array#indexOf and String#indexOf comparisons against -1 were the standard ways to check whether a value exists in an array or string. ES2015 added String.prototype.includes() and ES2016 added Array.prototype.includes(), which are more readable and expressive.

This rule reports when an .indexOf() comparison can be replaced with .includes(). It checks for any receiver object that has both .indexOf() and .includes() methods with compatible parameters.

Matching types include: String, Array, ReadonlyArray, and typed arrays.

declare const
const array: number[]
array
: number[];
declare const
const value: number
value
: number;
const array: number[]
array
.
Array<number>.indexOf(searchElement: number, fromIndex?: number): number

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

@paramsearchElement The value to locate in the array.

@paramfromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

indexOf
(
const value: number
value
) !== -1;
declare const
const array: number[]
array
: number[];
declare const
const value: number
value
: number;
const array: number[]
array
.
Array<number>.indexOf(searchElement: number, fromIndex?: number): number

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

@paramsearchElement The value to locate in the array.

@paramfromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

indexOf
(
const value: number
value
) === -1;
declare const
const array: number[]
array
: number[];
declare const
const value: number
value
: number;
const array: number[]
array
.
Array<number>.indexOf(searchElement: number, fromIndex?: number): number

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

@paramsearchElement The value to locate in the array.

@paramfromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

indexOf
(
const value: number
value
) > -1;
declare const
const array: number[]
array
: number[];
declare const
const value: number
value
: number;
const array: number[]
array
.
Array<number>.indexOf(searchElement: number, fromIndex?: number): number

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

@paramsearchElement The value to locate in the array.

@paramfromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

indexOf
(
const value: number
value
) >= 0;
declare const
const str: string
str
: string;
const str: string
str
.
String.indexOf(searchString: string, position?: number): number

Returns the position of the first occurrence of a substring.

@paramsearchString The substring to search for in the string

@paramposition The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.

indexOf
("test") !== -1;

This rule is not configurable.

If you need to support environments that don’t have Array.prototype.includes() (pre-ES2016) or String.prototype.includes() (pre-ES2015), or if you need the actual index returned by .indexOf(), you may disable this rule.

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