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.
Returns true if searchString appears as a substring of the result of converting this
object to a String, at one or more positions that are
greater than or equal to position; otherwise, returns false.
@param ― searchString search string
@param ― position If position is undefined, 0 is assumed, so as to search all of the String.
includes("test");
declare const
const array:number[]
array:number[];
declare const
const value:number
value:number;
const
const index:number
index =
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.
@param ― searchElement The value to locate in the array.
@param ― fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
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.