Skip to content

Bug: [no-unnecessary-condition] Unnecessary conditional when trying to use coalesce operator on null | undefined #10055

Closed as not planned
@darkbasic

Description

@darkbasic

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.5.2&fileType=.tsx&code=GYVwdgxgLglg9mABAWwJ4DFwQBQEMBOA5gFyJggA2FiAPouACYCmwMYTDAlKYy2x4gDeAKESJ8TKCHxIChRAH4F9MM1bsGwgL5A&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRgA2uGyRE0aB2iQANKuyYDaofHgcA7gGFJyfEJYAZDh2I3ps%2BU1QZ8cRMYQAL7GALqqIUFAA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function myFunc(arg: null | undefined): undefined {
  return arg ?? undefined
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-condition": [
    "error",
      {
      "allowConstantLoopConditions": true
      }
    ]
  },
};

tsconfig

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

Expected Result

arg ?? undefined limits the union type null | undefined to just undefined so it's not unnecessary.

Actual Result

arg ?? undefined errors out like if it didn't restrict the union type a subset of its values.

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions