elseReturns
Reports unnecessary
elseblocks followingifstatements that always return or throw.
✅ This rule is included in the ts stylisticStrict presets.
When an if block always ends with a terminating statement (return or throw), any code in the else block is unnecessary because it will only execute when the condition is false.
Removing the else clause reduces nesting and improves code readability.
This rule also handles:
throwstatements as terminating statements- Nested
if/elseblocks where the inner if-else always terminates (making the outer else unnecessary)
Examples
Section titled “Examples”function getValue(condition: boolean) { if (condition) { return 1; } else { return 2; }}function process(a: boolean, b: boolean) { if (a) { return compute(); } else if (b) { return fallback(); } else { return defaultValue(); }}function getValue(condition: boolean) { if (condition) { return 1; }
return 2;}function process(a: boolean, b: boolean) { if (a) { return compute(); }
if (b) { return fallback(); }
return defaultValue();}When Not To Use It
Section titled “When Not To Use It”If your team prefers the explicit structure of if/else blocks for clarity in conditional logic, or you have a large codebase where enforcing this style would require significant refactoring, you may choose to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noUselessElse - ESLint:
no-else-return - Oxlint:
eslint/no-else-return
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.