An array is filtered whole only to test for a match or take the first one

tx0/ts:filter-for-existencestrictwarningperformance

What it finds

items.filter(isOpen).length > 0 builds a whole array to learn whether one element matches, and items.filter(isOpen)[0] to take the first match; items.some(isOpen) and items.find(isOpen) stop at the first match and say what is asked.

Reported are .filter(...).length compared with 0 by >, !==, !=, === or ==, or with 1 by >=, and .filter(...)[0]. There is no fix, since the receiver may be an object other than an array.

From the rule's source, filter_for_existence.rs.

Example from the rule's tests
export function any(items: number[]) {  const a = items.filter((x) => x > 1).length > 0;tx0 reports: some stops at the first  const b = items.filter((x) => x > 1)[0];tx0 reports: find stops there  const c = items.filter((x) => x > 1).length > 2;  return [a, b, c];}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:filter-for-existence" = "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:filter-for-existence] reason="..."

tx0 explain tx0/ts:filter-for-existence shows the same in the terminal. Found a false positive? Report it.