Skip to content

Bug: [prefer-nullish-coalescing] Should not report on false | undefined values by default #11092

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

Open
4 tasks done
JoshuaKGoldberg opened this issue Apr 22, 2025 · 1 comment
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look

Comments

@JoshuaKGoldberg
Copy link
Member

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.8.2&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3gWygTwCMQAueAcijPgB9yCyBuAKFElgWXSwDMoIICUMAGtSAbTJgyAXWatoceNwCuKMBgCWqRHCgYQANT7KQaABQBKUphgaUAczHSa8VaG52QwZkxVrN2sjYAA7K%2BgDKGLYO5lbwNnaOzgDeTPCIqJjwdvow2F4aeggAvDj4RPDFVeSU8ABkdTogRUYQJrE%2B6ZxZAG7GJdkoufnAhfo0tLz8giKd8HAYyjAo8H1tIMwAvkxAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokcGAC%2BILUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const maybe: 'a' | 'b';
declare const fallback: ['c'];
declare function createValues(): string[] | undefined;

function computeStrings(): string[] {
  const intermediate = maybe === 'a' && createValues();

  const value = intermediate || fallback;

  return value;
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

intermediate is false | string[] | undefined and its parent function returns string[]. The || is the right operator to use to make value a string[], not a string[] | false.

Actual Result

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator. 8:30 - 8:32

Additional Info

Related: #7055, #10356, and probably others, searching gives a lot of false positive results...

#10356 notes that this gets suggested a good amount. I personally was surprised to re-discover that the default behavior of the rule is to report in a way that is sometimes incorrect/unnecessary.

Proposal: I think we should switch the rule to ignore false | undefined | (always-truthy)s by default.

💖

@JoshuaKGoldberg JoshuaKGoldberg added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 22, 2025
@kirkwaiblinger
Copy link
Member

I'd probably prefer ignorePrimitives: true being default and using strict-boolean-expressions to handle the primitive cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

2 participants