TS2366: Not All Code Paths Return: What It Means and How to Fix It
Error reference
TS2366: Not All Code Paths Return
Language
Severity
MediumWhen it happens
TypeScript error TS2366 is reported when a function is declared to return a specific type but has at least one code path that reaches the end of the function without a return statement.
Potential fixes
Add a final return statement that covers all unhandled cases. Consider throwing an error for truly unreachable paths to make the intent explicit. Use a switch with an exhaustive check or a lookup table to eliminate branching.
Related errors
Deep dive
TypeScript error TS2366 is reported when a function is declared to return a specific type but has at least one code path that reaches the end of the function without a return statement. The function would implicitly return undefined on that path.
Why it happens
- •A conditional chain does not cover all possible inputs with a return statement.
- •A switch statement is missing a default case that returns a value.
- •An if/else chain handles some cases but not all.
- •Early returns in try/catch blocks leave the end of the function reachable.
Potential fixes
Add a final return statement that covers all unhandled cases. Consider throwing an error for truly unreachable paths to make the intent explicit. Use a switch with an exhaustive check or a lookup table to eliminate branching.
Examples
Code that triggers the error
function getLabel(code: number): string {
if (code === 200) return "OK";
if (code === 404) return "Not Found";
// missing: no return for other codes
}Error output
error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
Fixed code
function getLabel(code: number): string {
if (code === 200) return "OK";
if (code === 404) return "Not Found";
return "Unknown";
}Practice in English
How would you explain a TS2366: Not All Code Paths Return to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free