indexOf is tested with > 0, which misses index 0, or searches for NaN, which it never finds

tx0/ts:index-of-misuserecommendedwarningbug

What it finds

Two ways to get indexOf wrong:

- name.indexOf(" ") > 0 misses a match at the start, at index 0. >= 0 or includes tests whether there is a match at all. - values.indexOf(NaN) is always -1, since indexOf compares with === and NaN equals nothing; includes and findIndex(Number.isNaN) find it.

lastIndexOf counts too. NaN must be the global one.

From the rule's source, index_of_misuse.rs.

Example from the rule's tests
function find(name: string, values: number[]) {  if (name.indexOf(" ") > 0) {tx0 reports: > 0` misses a match at index 0    return 1;  }  const i = values.indexOf(NaN);tx0 reports: indexOf never finds NaN  return name.indexOf(" ") >= 0 || i > 0;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:index-of-misuse" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:index-of-misuse] reason="..."

tx0 explain tx0/ts:index-of-misuse shows the same in the terminal. Found a false positive? Report it.