Closed
Description
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
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.