Linter · Rules
default-case
Require a default case in every switch
A switch without default does nothing when no case matches. When a new value shows up, it's silently skipped. A default makes you decide what happens then, even if the answer is to throw.
If you leave it out on purpose, say so with a // no default comment after the last case.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"default-case" = "error" # or "warn"
Every option, set to its default. The ones that are commented out are unset by default.
bunfig.toml
[lint.rules]
"default-case" = ["error", {
# commentPattern = <string>,
}]
Examples
Reported:
switch (status) {
case "open":
show();
break;
case "closed":
hide();
break;
}
Not reported:
switch (status) {
case "open":
show();
break;
case "closed":
hide();
break;
default:
throw new Error("Unknown status: " + status);
}
switch (status) {
case "open":
show();
break;
case "closed":
hide();
break;
// no default
}
Options
| Option | Default | What it does |
|---|---|---|
commentPattern | "^no default$", in any case | A regular expression for the comment that excuses a missing default. It's tested against the last comment after the last case. |
switch (status) {
case "open":
show();
break;
// skip default: other statuses are not shown
}
With your own pattern, // no default no longer counts:
switch (status) {
case "open":
show();
break;
// no default
}
When not to use it
In TypeScript, @typescript-eslint/switch-exhaustiveness-check reports every member of a union that has no case. It finds a new value when you lint. A default only finds it when the code runs.