persona-community-5/.pnpm-store/v3/files/ff/3605fa3c47b97457bcc24612225e50edeecacfacba2ab4592b45b2e4d55857aa789337647ede97dda782d57b7e44e3011305d0bb62567d321efca17ec574f1
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

82 lines
2.2 KiB
Plaintext

---
description: 'Enforce `includes` method over `indexOf` method.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/prefer-includes** for documentation.
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, respectively.
Alternatives that are easier to read and write now exist: ES2015 added `String#includes` and ES2016 added `Array#includes`.
This rule reports when an `.indexOf` call can be replaced with an `.includes`.
Additionally, this rule reports the tests of simple regular expressions in favor of `String#includes`.
> This rule will report on any receiver object of an `indexOf` method call that has an `includes` method where the two methods have the same parameters.
> Matching types include: `String`, `Array`, `ReadonlyArray`, and typed arrays.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
const str: string;
const array: any[];
const readonlyArray: ReadonlyArray<any>;
const typedArray: UInt8Array;
const maybe: string;
const userDefined: {
indexOf(x: any): number;
includes(x: any): boolean;
};
str.indexOf(value) !== -1;
array.indexOf(value) !== -1;
readonlyArray.indexOf(value) === -1;
typedArray.indexOf(value) > -1;
maybe?.indexOf('') !== -1;
userDefined.indexOf(value) >= 0;
/example/.test(str);
```
</TabItem>
<TabItem value="✅ Correct">
```ts
const str: string;
const array: any[];
const readonlyArray: ReadonlyArray<any>;
const typedArray: UInt8Array;
const maybe: string;
const userDefined: {
indexOf(x: any): number;
includes(x: any): boolean;
};
str.includes(value);
array.includes(value);
!readonlyArray.includes(value);
typedArray.includes(value);
maybe?.includes('');
userDefined.includes(value);
str.includes('example');
// The two methods have different parameters.
declare const mismatchExample: {
indexOf(x: unknown, fromIndex?: number): number;
includes(x: unknown): boolean;
};
mismatchExample.indexOf(value) >= 0;
```
</TabItem>
</Tabs>
{/* Intentionally Omitted: When Not To Use It */}