defaultCaseLast
Reports switch statements where the default clause is not last.
✅ This rule is included in the ts logical
preset.
When a default
clause appears before other case
clauses in a switch statement, it can lead to confusion about the control flow.
While the default
clause will still only execute when no other case matches, placing it in the middle or at the beginning makes the code harder to understand.
Convention and readability are improved by keeping the default
clause at the end of switch statements.
Examples
Section titled “Examples”function getDescription(value: number): string { switch (value) { default: return "unknown"; case 1: return "one"; case 2: return "two"; }}
function processValue(input: string): void { switch (input) { case "start": console.log("Starting..."); break; default: console.log("Processing..."); break; case "end": console.log("Ending..."); break; }}
function getDescription(value: number): string { switch (value) { case 1: return "one"; case 2: return "two"; default: return "unknown"; }}
function processValue(input: string): void { switch (input) { case "start": console.log("Starting..."); break; case "end": console.log("Ending..."); break; default: console.log("Processing..."); break; }}
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you have a specific code style guide that requires default clauses to appear in a different position, you might choose to disable this rule. However, this is generally not recommended as it goes against common JavaScript conventions.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useDefaultSwitchClauseLast
- ESLint:
default-case-last
- Oxlint:
eslint/default-case-last
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.