Skip to content

fix(prefer-array-some): skip find() fixes when elements may be falsy - #183

Closed
kelvindesman wants to merge 1 commit into
e18e:mainfrom
kelvindesman:fix/prefer-array-some-falsy-elements
Closed

kelvindesman wants to merge 1 commit into
e18e:mainfrom
kelvindesman:fix/prefer-array-some-falsy-elements

Conversation

@kelvindesman

@kelvindesman kelvindesman commented Nov 10, 2026 •

Copy link
Copy Markdown

Fixes #168

Following the suggestion in the issue: when type information is available, prefer-array-some no longer reports find() checks where switching to some() would change the result.

  • Truthiness checks (if (arr.find(fn)), !arr.find(fn), !!arr.find(fn)) are skipped when the element type can be falsy: 0, '', false, null, undefined or 0n, which also covers number (and so NaN), string, boolean, any, unknown and unconstrained type parameters.
  • arr.find(fn) === undefined / !== undefined is skipped when the element type can be undefined.
  • filter(fn).length checks are unaffected, since they don't depend on the element's value.
  • Without type information nothing changes, as discussed in the issue.

I added canArrayContainFalsy and canArrayContainUndefined to src/utils/typescript.ts. They check whether a falsy literal type (checker.getFalseType(), getNumberLiteralType(0), etc.) is assignable to the element type, rather than reading TypeFlags, because the flag values changed in TypeScript 6. Type parameters use their constraint, so T[] and T extends number[] work too.

Known limits:

  • Branded primitives (string & {__brand: 'x'}) and string enums with an empty member are still reported, because '' isn't assignable to them.
  • With strictNullChecks off, null and undefined are assignable to every type, so they're left out of the check there (otherwise every typed array would be skipped). The === undefined path keeps the old behaviour in that mode. I couldn't get the rule tester to load a non-strict tsconfig, so that branch isn't covered by a test.

Tests: new typed cases in prefer-array-some.test.ts, covering number, string, boolean, bigint, any, unknown, tuples, unions of arrays, readonly arrays, enums with and without a 0 member, generic T[] and T extends number[], and both === undefined directions. 19 of them fail without the change. npm run build && npm run lint && npm test pass locally (1090 tests).

I used Claude Code to help with this, and I reviewed the change and the tests myself.

🤖 Generated with Claude Code

With type information, don't report find() truthiness checks when the array element type can be falsy, or find() === undefined checks when it can be undefined, since some() would give a different result. Without types the behaviour is unchanged.

Fixes e18e#168

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@43081j

43081j commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

the problem with having an agent take a stab at an unresolved issue: the issue is unresolved 😅

we still need to settle on an approach in the issue.

claude has also approached this the wrong way, but i'm not going to review as i don't want another claude iteration, i want a discussion in the issue. let's discuss there until we've decided a solution

@kelvindesman

Copy link
Copy Markdown
Author

Fair point, sorry about that. I jumped ahead of the discussion. Closing this; I'll follow the conversation in #168 and only come back with code once there's an agreed approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prefer-array-some: fix changes the result when find() is used as a truthy check

2 participants