Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [switch-exhaustiveness-check] requireDefaultForNonUnion false positive on type with one value #8238

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

Closed
4 tasks done
skyqrose opened this issue Jan 11, 2024 · 0 comments · Fixed by #8250
Closed
4 tasks done
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

@skyqrose
Copy link

skyqrose commented Jan 11, 2024

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.3.3&fileType=.tsx&code=C4TwDgpgBAsiAq5oF4oCIA2BLYEBOAhhlAM7B5YB2A5mlAPQBUUAPugPbAAW%2Bdj9AbgBQQiAA8w7PMCgBjdpTJQAZlFQAKMQC5YCJAEo1APigBvIVFIB3HLK5RNh85cuyCJaJhz4ipclVotCxdLPAhgAFc8SmEQpmDXd09OHjw0IJDQ8KiYhKh%2BYIBfIULhIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6ZAd0vzIAt6AHrwCGsZPkoA3RExTJafRGQDW6MAG1w2HB2HQmkADRbtWbdkjD48APYcAIogBmo%2BPgDCw5IgBiN6ACiQqLiUogAylw8vGou8N7G5hbQiACOsJQpji4I%2BH7QAHI2TACqTJTFavhwiCbYAL51ALpajfVAA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

type MyType = "literal string" /* | "other" */;

export const f = (x: MyType) => {
  switch (x) {
    case "literal string":
      return;
    /*
    case "other":
      return;
    */
  }
};

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/switch-exhaustiveness-check": [
      "warn",
      {
        allowDefaultCaseForExhaustiveSwitch: false,
        requireDefaultForNonUnion: true,
      },
    ],
  },
};

tsconfig

No response

Expected Result

No warning. This switch exhaustively covers the 1 case.

Actual Result

A warning.

Additional Info

Convenient link to the docs. This option was added recently: #7880

The option works for unions of strings, but if the union only has one option (so it's just a literal), then the option thinks the switch needs a default.

You might wonder why we have a switch that only has one case. This is code that changes frequently for handling feature flags in our app, and right now, we only have one feature flag, but don't want to refactor out the switch just to add it back when we add a second flag again.

In the example, if you uncomment the second option, it will correctly know there doesn't need to be a default, and eslint will pass.

@skyqrose skyqrose 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 Jan 11, 2024
@bradzacher bradzacher 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 Jan 12, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 7, 2024
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