Skip to content

elseReturns

Reports unnecessary else blocks following if statements 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:

  • throw statements as terminating statements
  • Nested if/else blocks where the inner if-else always terminates (making the outer else unnecessary)
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();
}
}

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.