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.
AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb53[<];d ivclassNa="meec-li">AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb5];d><3;d ivclassNa="meec-li">53arrAB0index53(value<) sp ansty="le--0:#C792EA;--1:#8844">AE!) sp><==sp ansty="le--0:#D6DEEB;--1:#403F">5==sp><3 sp ansty="le--0:#C792EA;--1:#8844">A3 sp>8<21sp ansty="le--0:#D6DEEB;--1:#403F">521sp>3;d><3;d ivclassNa="meco"><1;d>1;d>3;d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb53[<];d ivclassNa="meec-li">AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb5];d><3;d ivclassNa="meec-li">53arrAB0index53(value<) sp ansty="le--0:#C792EA;--1:#8844">AE=) sp><==sp ansty="le--0:#D6DEEB;--1:#403F">5==sp><3 sp ansty="le--0:#C792EA;--1:#8844">A3 sp>8<21sp ansty="le--0:#D6DEEB;--1:#403F">521sp>3;d><3;d ivclassNa="meco"><1;d>1;d>3;d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb53[<];d ivclassNa="meec-li">AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb5];d><3;d ivclassNa="meec-li">53arrAB0index53(value<) sp ansty="le--0:#C792EA;--1:#8844">A) sp>sp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#C792EA;--1:#8844">A3 sp>8<21sp ansty="le--0:#D6DEEB;--1:#403F">521sp>3;d><3;d ivclassNa="meco"> -"><1;d>1;d>3;d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb53[<];d ivclassNa="meec-li">AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0numb5];d><3;d ivclassNa="meec-li">53arrAB0index53(value<) sp ansty="le--0:#C792EA;--1:#8844">AE) sp><>=sp ansty="le--0:#D6DEEB;--1:#403F">5>=sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">83 sp><20sp ansty="le--0:#D6DEEB;--1:#403F">520sp>3;d><3;d ivclassNa="meco">= "><0;d>0;d>3;d>AEdeclare consB0s7<2:sp ansty="le--0:#C792EA;--1:#8844">A2:sp>B0stri5<3;d ivclassNa="meec-li">53sAB0index5<3(sp ansty="le--0:#D9F5DD;--1:#1111">13(sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dte1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">531"sp><) sp ansty="le--0:#C792EA;--1:#8844">AE!) sp><==sp ansty="le--0:#D6DEEB;--1:#403F">5==sp><3 sp ansty="le--0:#C792EA;--1:#8844">A3 sp>8<21sp ansty="le--0:#D6DEEB;--1:#403F">521sp>3;d><3;d ivclassNa="meco"><1;d>1;d>3;d> 44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >3F5<3[] ;divclass="Nameec-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >33[]>3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0incl4AE.>3F53(val<;di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >3F5<3[] ;divclass="Nameec-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >33[]>4<4AE! spans="tyle--0:#D6DEEB;--1:#40">3F53a4AE!>4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0incl4AE.>3F53(val;di><;di vdivclass="Nameexpressive-">44AEdeclare co61B<0str spans="tyle--0:#7FDBCA;--1:#09">60str>4<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4AE >33F5<3str spans="tyle--0:#C792EA;--1:#88">43str><4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0incl4AE.>31<111" spans="tyle--0:#ECC48D;--1:#98">4E4D111">1<111" spans="tyle--0:#D6DEEB;--1:#40">3F111"><53) ;divclass="Name">53)>;di><;di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >3F5<3[] ;divclass="Nameec-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4AE >33[]>44AEco61B0i44A3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0ind4AE.>3F53(val;di>
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.