Lingua-e
JavaScript

What is a TS2366: Not All Code Paths Return?

← Errors

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.

Examples

JavaScript

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'.

How to fix it

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.

Fixed code

function getLabel(code: number): string {
  if (code === 200) return "OK";
  if (code === 404) return "Not Found";
  return "Unknown";
}

Related errors

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