Skip to content
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

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

Closed
4 tasks done
MBuchalik opened this issue Jul 22, 2023 · 0 comments · Fixed by #7466
Closed
4 tasks done

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

MBuchalik opened this issue Jul 22, 2023 · 0 comments · Fixed by #7466
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

@MBuchalik
Copy link
Contributor

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.

@MBuchalik MBuchalik added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jul 22, 2023
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Jul 22, 2023
MBuchalik added a commit to MBuchalik/typescript-eslint that referenced this issue Aug 13, 2023
MBuchalik added a commit to MBuchalik/typescript-eslint that referenced this issue Aug 13, 2023
JoshuaKGoldberg pushed a commit that referenced this issue Aug 19, 2023
…randed types (#7466)

* fix(eslint-plugin): [no-unnecessary-condition] false positives with branded types (#7293)

* Improve test coverage and handle branded types in 'isPossiblyTruthy'
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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