10000 Bug: [switch-exhaustiveness-check] requireDefaultForNonUnion false positive on type with one value · Issue #8238 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
Bug: [switch-exhaustiveness-check] requireDefaultForNonUnion false positive on type with one value #8238
Closed
@skyqrose

Description

@skyqrose

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.

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

      0