Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@azat-io
Copy link
Contributor

@azat-io azat-io commented Nov 3, 2025

PR Checklist

Overview

Adds detection of redundant Array.isArray() calls when checkTypePredicates is enabled.

Reports error when the argument is already typed as an array (including readonly arrays).

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @azat-io!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Nov 3, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit e1abd45
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/691102592233790008c45bb9
😎 Deploy Preview https://deploy-preview-11735--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 3 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link

nx-cloud bot commented Nov 3, 2025

View your CI Pipeline Execution ↗ for commit e1abd45

Command Status Duration Result
nx test eslint-plugin --coverage=false ✅ Succeeded 5m 13s View ↗
nx run-many -t lint ✅ Succeeded 3m 15s View ↗
nx run-many -t typecheck ✅ Succeeded 2m 2s View ↗
nx test eslint-plugin-internal --coverage=false ✅ Succeeded 3s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded 2s View ↗
nx run types:build ✅ Succeeded 5s View ↗
nx run integration-tests:test ✅ Succeeded 3s View ↗
nx run generate-configs ✅ Succeeded 6s View ↗
Additional runs (29) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-11-09 21:19:28 UTC


{
code: `
const items: number[] | null = [1, 2, 3];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to do it like this, I think, here and elsewhere:

Suggested change
const items: number[] | null = [1, 2, 3];
declare const items: number[] | null;

TS ignores the | null because it can see that it's not null (playgroun)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I replace this entire diff with

-          if (typeOfArgument === typeGuardAssertedArgument.type) {
+          if (
+            checker.isTypeAssignableTo(
+              typeOfArgument,
+              typeGuardAssertedArgument.type,
+            )
+          ) {

(which is what was intended in #11716 (comment)), the only test case that doesn't pass (apart from spurious reasons) is

function processArray(arr: readonly string[]) {
  if (Array.isArray(arr)) {
    return arr.length;
  }
}

So, I'm still thinking that changing to the assignability API makes sense, at least as a first step. In other words, let's take on the assignability API change before worrying about the edge case of readonly arrays with Array.isArray() (which, FWIW, I think is basically a corollary of microsoft/TypeScript#17002). What do you think?

Also cc @ronami

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed to use checker.isTypeAssignableTo() for Array.isArray

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io almost! I really mean the entire diff, though, not just the Array.isArray case. The thought is to change all unnecessary type predicate detection to be based on assignability.

Note that this change would require accompanying report message changes and additional testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Changed to use isTypeAssignableTo() for all type predicates. Removed isArrayIsArrayCall() function and updated the tests accordingly.

@codecov
Copy link

codecov bot commented Nov 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.67%. Comparing base (189a7f7) to head (e1abd45).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11735   +/-   ##
=======================================
  Coverage   90.66%   90.67%           
=======================================
  Files         518      518           
  Lines       52444    52478   +34     
  Branches     8690     8701   +11     
=======================================
+ Hits        47548    47584   +36     
+ Misses       4882     4880    -2     
  Partials       14       14           
Flag Coverage Δ
unittest 90.67% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...slint-plugin/src/rules/no-unnecessary-condition.ts 99.59% <100.00%> (+0.01%) ⬆️
...ckages/rule-tester/src/utils/flat-config-schema.ts 40.65% <ø> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kirkwaiblinger kirkwaiblinger changed the title feat(eslint-plugin): [no-unnecessary-condition] detect unnecessary Array.isArray calls feat(eslint-plugin): [no-unnecessary-condition] flag unnecessary type predicates based on assignability Nov 3, 2025
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have time to do a fully detailed review right now, but jotting down the notes that I'll want to come back to. Feel free to look into these in the meantime:

  1. We'll probably need to update the text of the typeGuardAlreadyIsType message.
  2. The getConstrainedTypeAtLocation may actually be wrong/unnecessary now. Needs tests with generics to prove one way or another.
  3. Like with no-unsafe-type-assertion, we should double-check and add testing around object literals and excess property checking.
  4. Needs further tests around subtype cases.

@azat-io azat-io force-pushed the feat/no-unnecessary-condition-array branch 2 times, most recently from a9234f9 to 5bb6d0a Compare November 9, 2025 19:08
@azat-io azat-io force-pushed the feat/no-unnecessary-condition-array branch 4 times, most recently from 7224f53 to c0cfba1 Compare November 9, 2025 20:42
@azat-io azat-io force-pushed the feat/no-unnecessary-condition-array branch from c0cfba1 to e1abd45 Compare November 9, 2025 21:06
@azat-io
Copy link
Contributor Author

azat-io commented Nov 9, 2025

Dialed the change back to focus solely on Array.isArray: the rule still reports when the argument type exactly matches the predicate type, and now additionally reports redundant Array.isArray checks when the argument is assignable to the guard’s array type (excluding any/unknown). Added valid/invalid coverage for unions, generics, and tuples so the new path stays constrained.

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.

Enhancement: [no-unnecessary-condition] detect unnecessary calls to Array.isArray

2 participants