Bug: [switch-exhaustiveness-check] requireDefaultForNonUnion false positive on type with one value #8238
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
Uh oh!
There was an error while loading. Please reload this page.
Before You File a Bug Report Please Confirm You Have Done The Following...
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
ESLint Config
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.
The text was updated successfully, but these errors were encountered: