Skip to content
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 non-nullable #8262

Merged
merged 3 commits into from Jan 28, 2024

Conversation

auvred
Copy link
Member

@auvred auvred commented Jan 16, 2024

PR Checklist

Overview

#8089 introduced change in the isNullableType function. It started treating any and unknown as nullables, it worked for no-unnecesary-type-assertion, but not for prefer-nullish-coalescing. prefer-nullish-coalescing expects isNullableType to return true only for null and undefined

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @auvred!

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 Jan 16, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit ce3d8e0
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65b1199a8d438a00087a70f7
😎 Deploy Preview https://deploy-preview-8262--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: 90 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (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.

@Josh-Cena
Copy link
Member

Josh-Cena commented Jan 17, 2024

I'm good with this solution 👍 I wonder though, shouldn't we recommend switching to nullish coalescing for any/unknown too, with an option to turn that off, just like primitives? Because any/unknown are also nullable, it doesn't make no sense to use nullish coalescing.

@dacarley
Copy link

Has anyone confirmed that this change fixes #8261 and #8276? If not, I may try to confirm it tomorrow.

If confirmed, can the final question about nullish coalescing be deferred into a new PR?

@@ -309,7 +309,7 @@ export default createRule<Options, MessageIds>({
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode.left);
if (!isNullableType(type)) {
if (!isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)) {
Copy link
Member

Choose a reason for hiding this comment

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

@Josh-Cena threading #8262 (comment):

I'm good with this solution 👍 I wonder though, shouldn't we recommend switching to nullish coalescing for any/unknown too, with an option to turn that off, just like primitives? Because any/unknown are also nullable, it doesn't make no sense to use nullish coalescing.

Hmm, good question. I think if the type is explicitly not known then it'd be risky to suggest making any concrete changes to its handling. I'd be in support of this as an opt-in in for v6 and maybe an opt-out for v7.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Code changes LGTM! Just requesting the tests declare their variables fully. Thanks!

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jan 24, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jan 24, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Swell, thanks!

@JoshuaKGoldberg JoshuaKGoldberg merged commit 70505e4 into typescript-eslint:main Jan 28, 2024
58 checks passed
@ArnaudBarre
Copy link
Contributor

ArnaudBarre commented Jan 29, 2024

I can't comment on the orignal PR but I don't really like the change introduce in #8089

The only reason is that it fixes an issue that I don't understand because any assertion on unkown or any is effectively useless, you can look at the inferred type here: https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwDYEMDOa4EE4G8BQccAJijCgPwBccArgHYDW9EA7vQNz4C+++okWHHQBPegjgAzBghgBLCPTgxgaGAAoAlHkJwkweCho4AvHHrBW2LVyJQDtKEpQA6UuS68gA

Screenshot of a the TS playground linked above with the inferred type being Promise<unkown>

Copy link

Uh oh! @ArnaudBarre, the image you shared is missing helpful alt text. Check #8262 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

danvk pushed a commit to danvk/typescript-eslint that referenced this pull request Feb 4, 2024
…non-nullable (typescript-eslint#8262)

* fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as non-nullable

* chore: rm unrelated changes

* test: add declarations of 'y' variable
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
5 participants