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

Skip to content

fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as eligible for nullish coalescing #10865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kirkwaiblinger
Copy link
Member

@kirkwaiblinger kirkwaiblinger commented Feb 21, 2025

PR Checklist

Overview

Rather than creating a separate "ignorePrimitives"-like option for any/unknown, I interpreted it that any/unknown will only be eligible for conversion if all "ignorePrimitive" options are false, since that matches the assignability.

Also, I

  • moved the isPossiblyNullish utility back to no-unnecessary-condition since it differs from isNullableType, and isNullableType is more reliable.
  • Fixed some examples on the docs page that this change exposed

cc @OlivierZal

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

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.

Copy link

netlify bot commented Feb 21, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit bea323d
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/67c9d54ae1027b0008ea9878
😎 Deploy Preview https://deploy-preview-10865--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: 72 (🔴 down 26 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (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 site configuration.

Copy link

nx-cloud bot commented Feb 21, 2025

View your CI Pipeline Execution ↗ for commit bea323d.

Command Status Duration Result
nx test eslint-plugin ✅ Succeeded 8m 1s View ↗
nx run eslint-plugin:test -- --coverage ✅ Succeeded 6m 4s View ↗
nx test eslint-plugin --coverage=false ✅ Succeeded 6m 14s View ↗
nx test visitor-keys ✅ Succeeded <1s View ↗
nx run types:build ✅ Succeeded <1s View ↗
nx test utils ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude website --... ✅ Succeeded 20s View ↗
nx test type-utils ✅ Succeeded 1s View ↗
Additional runs (25) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-03-06 17:18:02 UTC

Copy link

codecov bot commented Feb 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.48%. Comparing base (7f2a5bd) to head (bea323d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10865   +/-   ##
=======================================
  Coverage   87.48%   87.48%           
=======================================
  Files         469      469           
  Lines       16089    16089           
  Branches     4666     4667    +1     
=======================================
  Hits        14075    14075           
  Misses       1658     1658           
  Partials      356      356           
Flag Coverage Δ
unittest 87.48% <100.00%> (ø)

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.21% <100.00%> (+0.01%) ⬆️
...lint-plugin/src/rules/prefer-nullish-coalescing.ts 95.65% <100.00%> (+0.07%) ⬆️
packages/eslint-plugin/src/util/truthinessUtils.ts 100.00% <ø> (ø)
🚀 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.

Copy link
Member

@ronami ronami left a comment

Choose a reason for hiding this comment

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

👍👍 Looking good! Requesting minor changes, mostly around testing.

// won't be set.
(tsutils.isTypeFlagSet(
type,
ts.TypeFlags.Any | ts.TypeFlags.Undefined | ts.TypeFlags.Void,
Copy link
Member

Choose a reason for hiding this comment

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

[Test]: Removing ts.TypeFlags.Void or ts.TypeFlags.Undefined doesn't fail any test. Let's simplify or add a test case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Touche - undefined was a typo and should have been unknown 😅 . That resolves #10865 (comment). void is a TS error. removed!

.typeParts(type)
.some(t =>
tsutils
.intersectionTypeParts(t)
Copy link
Member

Choose a reason for hiding this comment

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

[Test]: Can this be simplified to use .typeParts() instead of .intersectionTypeParts()? Changing this doesn't seem to fail any test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe... I think I'd like to leave that line as is since technically it wasn't touched in this PR though. (The change that I had to make was because the type assertion type as ts.UnionOrIntersectionType was no longer valid). It's generally a bit of a smell I think if we treated unions and intersections the same shallowly, but not recursively 🤔 But I don't have a test case in mind

// won't be set.
(tsutils.isTypeFlagSet(
type,
ts.TypeFlags.Any | ts.TypeFlags.Undefined | ts.TypeFlags.Void,
Copy link
Member

Choose a reason for hiding this comment

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

[Bug]: To my understanding, an unknown type with any of the ignorePrimitives options set to true shouldn't be reported (deploy preview playground link):

// ignorePrimitives: /* any set to true */

// passes correctly with `any`
declare const u1: any;
const foo1 = u1 || 'bar'

// should pass with `unknown`?
declare const u2: unknown;
const foo2 = u2 || 'bar'

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch! should be fixed now 👍

@ronami ronami added the awaiting response Issues waiting for a reply from the OP or another party label Mar 1, 2025
kirkwaiblinger and others added 2 commits March 3, 2025 00:53
Co-authored-by: Ronen Amiel <[email protected]>
@kirkwaiblinger kirkwaiblinger requested a review from ronami March 3, 2025 08:21
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Mar 3, 2025
ronami
ronami previously approved these changes Mar 3, 2025
Copy link
Member

@ronami ronami left a comment

Choose a reason for hiding this comment

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

👍👍

@ronami ronami added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 3, 2025
@OlivierZal
Copy link
Contributor

FWIW it also looks good to me 😊

@kirkwaiblinger
Copy link
Member Author

had to fix up a nasty merge conflict, but should be good to again! 👍

@kirkwaiblinger kirkwaiblinger requested a review from ronami March 4, 2025 00:50
@github-actions github-actions bot removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 4, 2025
ronami
ronami previously approved these changes Mar 5, 2025
Copy link
Member

@ronami ronami left a comment

Choose a reason for hiding this comment

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

🚀🚀🚀

@ronami ronami added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 5, 2025
Co-authored-by: Ronen Amiel <[email protected]>
@kirkwaiblinger kirkwaiblinger requested a review from ronami March 6, 2025 00:50
@github-actions github-actions bot removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 6, 2025
@ronami ronami added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 6, 2025
@kirkwaiblinger kirkwaiblinger enabled auto-merge (squash) March 6, 2025 17:02
@kirkwaiblinger kirkwaiblinger merged commit dba0bed into typescript-eslint:main Mar 6, 2025
56 checks passed
OlivierZal added a commit to OlivierZal/typescript-eslint that referenced this pull request Mar 6, 2025
… as eligible for nullish coalescing (typescript-eslint#10865)

* treat any/unknown as eligible for nullish coalescing

* more stuff

* fix typo

Co-authored-by: Ronen Amiel <[email protected]>

* feedback

* tweaks

* merge mistake

* rename vars in test

Co-authored-by: Ronen Amiel <[email protected]>

---------

Co-authored-by: Ronen Amiel <[email protected]>
aryaemami59 pushed a commit to aryaemami59/typescript-eslint that referenced this pull request Mar 7, 2025
… as eligible for nullish coalescing (typescript-eslint#10865)

* treat any/unknown as eligible for nullish coalescing

* more stuff

* fix typo

Co-authored-by: Ronen Amiel <[email protected]>

* feedback

* tweaks

* merge mistake

* rename vars in test

Co-authored-by: Ronen Amiel <[email protected]>

---------

Co-authored-by: Ronen Amiel <[email protected]>
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Mar 11, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.26.0 | 8.26.1 |
| npm        | @typescript-eslint/parser        | 8.26.0 | 8.26.1 |


## [v8.26.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8261-2025-03-10)

##### 🩹 Fixes

-   **eslint-plugin:** \[no-unsafe-return] handle recursive type ([#10883](typescript-eslint/typescript-eslint#10883))
-   **eslint-plugin:** \[prefer-nullish-coalescing] treat `any`/`unknown` as eligible for nullish coalescing ([#10865](typescript-eslint/typescript-eslint#10865))

##### ❤️ Thank You

-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)
-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)

You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Mar 13, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.26.0 | 8.26.1 |
| npm        | @typescript-eslint/parser        | 8.26.0 | 8.26.1 |


## [v8.26.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8261-2025-03-10)

##### 🩹 Fixes

-   **eslint-plugin:** \[no-unsafe-return] handle recursive type ([#10883](typescript-eslint/typescript-eslint#10883))
-   **eslint-plugin:** \[prefer-nullish-coalescing] treat `any`/`unknown` as eligible for nullish coalescing ([#10865](typescript-eslint/typescript-eslint#10865))

##### ❤️ Thank You

-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)
-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)

You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2025
@kirkwaiblinger kirkwaiblinger deleted the prefer-nullish-coalescing-possibly-nullish branch March 28, 2025 19:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [prefer-nullish-coalescing] revisit considering any/unknown possibly-nullish
3 participants