useIncludes
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useIncludes - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@typescript-eslint/prefer-includes
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useIncludes": "error" } } }}Description
Section titled “Description”Prefer Array#includes() over Array#indexOf() checks.
Array#indexOf() returns a numeric index and is commonly compared against -1 to check
for the presence of an element. Array#includes() is more readable and expressive for
this purpose, and avoids off-by-one mistakes with the comparison operator.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const arr = [1, 2, 3];arr.indexOf(1) !== -1;/invalid1.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) !== -1;
│ ^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·!==·-1;
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];arr.indexOf(1) >= 0;/invalid2.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) >= 0;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·>=·0;
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];arr.indexOf(1) === -1;/invalid3.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) === -1;
│ ^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use !…includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with !…includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·===·-1;
2 │ + !
3 │ + arr.includes(1);
3 4 │
const arr = [1, 2, 3];arr.includes(1);const arr = [1, 2, 3];// Positional use of indexOf is fineconst pos = arr.indexOf(1);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.