Skip to content

Bug: [no-unnecessary-condition] False positives for branded strings #7293

Closed
@MBuchalik

Description

@MBuchalik

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.1.6&fileType=.tsx&code=C4TwDgpgBAsiBCAnAhgOwCYXQZWIglqgOZQC8UAznoSQGRQDeUA%2BswEYoYBcUArqgGtUAewDuqKAF8A3AFgAUAoDGw1FSgc0mHNWJkoAIgNRkFWAk7bcBYnPn4AZlAAUmjFms0AlIwVQoAPQBUAAiwpTCALYQwAAWNAB0CpIKCkFQACrxZujCEGYiwFAANhDI6FDA4cglhMAQiFANiMKISfIqakWIEES8xciInnrkRiZmVDZEdo4uPX0DQ7pEPgx%2BgcFhEdFxiclAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

type MyBrandedString = string & { __brand: unknown };

const brandedString = "" as MyBrandedString;
if (brandedString) {
  // Do something.
}

ESLint Config

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

tsconfig

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

Expected Result

I did not expect to get a linter error, since brandedString in the example can be falsy.

Actual Result

The rule reports:

Unnecessary conditional, value is always truthy.

This looks like a false positive to me, because string & { } does not disallow empty strings or the like.

Additional Info

#2506 could be somewhat related, since they also had issues with a similar intersection.

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