ProjectStack
typescript

TS2366: Function lacks ending return statement and return type does not include 'undefined'

Your function's declared return type doesn't include undefined, but the function can fall off the end without returning anything. TypeScript requires the return type to account for all possible exit paths, including a missing return.

Common causes

  • The return type is explicitly declared (e.g., string) but not all branches return that type
  • An if-else chain missing the else branch return with an explicit return type
  • A function that conditionally returns early but has no return for the default case
  • Strict mode requiring all declared return types to be fully honoured on every path

How to fix it

  1. Add a return at the end of the function for the uncovered code path
  2. Change the return type to include undefined: string | undefined
  3. Restructure the function so every branch explicitly returns
  4. Add a throw to signal an impossible state: throw new Error('Unexpected state')

Example

function getStatus(code: number): string { if (code === 200) return 'ok'; if (code === 404) return 'not found'; // error TS2366: Function lacks ending return statement and return type does not include 'undefined'. }

Return type is string but there's no catch-all return for unrecognized status codes

Browse more errors

The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.