Skip to content

Bug: @typescript-eslint/prefer-nullish-coalescing mal-fixes nested condition #11097

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
SimonSimCity opened this issue Apr 23, 2025 · 0 comments · May be fixed by #11098
Open
4 tasks done

Bug: @typescript-eslint/prefer-nullish-coalescing mal-fixes nested condition #11097

SimonSimCity opened this issue Apr 23, 2025 · 0 comments · May be fixed by #11098
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@SimonSimCity
Copy link

SimonSimCity commented Apr 23, 2025

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.7.2&showAST=types&fileType=.tsx&code=DYUwLgBAhgXBDOYBOBLAdgcwgHwgVzQBMQAzdEQgKFEgCM4BvCAWxHnigxDkVUwgC%2BOfEVLkqAYwD2aRBEgBeCAAowPZOgwBKCAoB88ypWmzIJKVN3RKEW3YD81u3bi0bzhxFoA6Vu04g7h4QcGDKAEQYwFK0UMDeIEhIUkjeBADWaFIA7mjhWpRAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokcGAC%2BILUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

let a: string | undefined
let b: { message: string } | undefined
const t = (t: string) => t

const foo = a
      ? a
      : b
        ? b.message
        : t("global.error.unknown")

The error happens when running the Fix on this rule: It breaks the code.

ESLint Config

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

tsconfig

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

Expected Result

let a: string | undefined
let b: { message: string } | undefined
const t = (t: string) => t

const foo = a ?? (b
        ? b.message
        : t("global.error.unknown"))

Actual Result

let a: string | undefined
let b: { message: string } | undefined
const t = (t: string) => t

const foo = a ?? b
        ? b.message
        : t("global.error.unknown")

Additional Info

No response

@SimonSimCity SimonSimCity 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 23, 2025
@kirkwaiblinger kirkwaiblinger added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants