🌐 AI搜索 & 代理 主页
Skip to content

Bug: [prefer-nullish-coalescing] ignoreBooleanCoercion should not apply to ternary expressions #11581

@kirkwaiblinger

Description

@kirkwaiblinger

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.9.2&fileType=.ts&code=CYUwxgNghgTiAEYD2A7AzgF3lAXPTMAligObwA%2B8GMArgpTSqAGbEjADcAUKJLAsnRYARngLEylYUiQQQUFBXiMWbTly6DM8AB7wAvPABCMuQoAUUeAH5s8PMICUHIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6MAG1w2HAHdJ0JpAA0a9VnXZIleU3GcAQuPFJJTAMLjec8boz44iQ9gC%2BfgC6aoH%2BQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare const a: string | true | undefined;
declare const b: string | boolean | undefined;

const x = Boolean(a ? a : b);

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": [
      "warn",
      {
        "ignoreBooleanCoercion": true
      }
    ]
  }
}

tsconfig

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

Expected Result

ternary should be reported

Actual Result

ternary is not reported

Additional Info

The motivation for ignoreBooleanCoercion was that it's helpful sometimes to write Boolean(a || b || c || d) rather than being forced into Boolean(a) || Boolean(b) || Boolean(c) || Boolean(d) (see #9080). I don't think that that is true of Boolean(a ? a : b ? b : c ? c : d)

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions