-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fix(eslint-plugin): [prefer-nullish-coalescing] treat any
/unknown
as eligible for nullish coalescing
#10865
Conversation
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. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
View your CI Pipeline Execution ↗ for commit bea323d.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this 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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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 👍
Co-authored-by: Ronen Amiel <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍👍
FWIW it also looks good to me 😊 |
had to fix up a nasty merge conflict, but should be good to again! 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀🚀🚀
packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Ronen Amiel <[email protected]>
… 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]>
… 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]>
| 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.
| 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.
PR Checklist
any
/unknown
possibly-nullish #10694Overview
Rather than creating a separate "ignorePrimitives"-like option for
any
/unknown
, I interpreted it thatany
/unknown
will only be eligible for conversion if all "ignorePrimitive" options are false, since that matches the assignability.Also, I
isPossiblyNullish
utility back to no-unnecessary-condition since it differs fromisNullableType
, andisNullableType
is more reliable.cc @OlivierZal