arrayExistenceChecksConsistency
Reports inconsistent styles for checking element existence using index methods.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
Enforces consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex().
Prefer using index === -1 to check if an element does not exist and index !== -1 to check if an element does exist.
These methods return -1 when an element is not found, so checking against -1 is more explicit and precise than using broad comparisons like < 0 or >= 0.
Examples
Section titled “Examples”const index = values.indexOf("test");if (index < 0) { // not found}const index = values.indexOf("test");if (index >= 0) { // found}const index = values.indexOf("test");if (index > -1) { // found}const index = values.indexOf("test");if (index === -1) { // not found}const index = values.indexOf("test");if (index !== -1) { // found}if (values.findIndex((value) => value > 10) === -1) { // not found}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase has an established convention using < 0 or >= 0 for index checks, you may disable this rule.
Some developers find the comparison style more readable in certain contexts.