Skip to content

Bug: [no-unnecessary-condition] Confusing message when comparing a variable to all possible values #9643

Closed
@StyleShit

Description

@StyleShit

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.4.5&fileType=.tsx&code=KYOwrgtgBAYgTmAlgFygbwFBSgQQA54A2wANFlAEICGINVGAvhhgCbADGhVcwU7A9iADOqAGYIUALlgTkAbmaJRUABTikqALzaZGgHT4ivAD7Go6lFG2bdKPdVq0AlOnIB6N1D3fGzZAE88XgB5ZAALYDh4DSsoAHIqAmI4qDM4gCM6WjiFVg4uHj5BESh%2BcMjoqShQiKjZXKVVMtrKrR0EpOAU01LyupjreMzHKjiXTGwPLx8GIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

type Fruit = 'apple' | 'banana';

declare const fruit: Fruit;

if (fruit === 'apple' || fruit === 'banana') {
  // ...
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

A message that tells me that the condition is always truthy because I'm comparing the variable to all its valid values

Actual Result

Getting a confusing message, only on the last part of the condition, saying that both sides are literal

Additional Info

I encountered this message here, which made me confused for a bit 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething 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-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions