Special Sponsor:PromptBuilder— Fast, consistent prompt creation powered by 1,000+ expert templates.
Make your Product visible here.Contact Us

Home/TypeScript Errors/TS2366

TS2366

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

TypeScript TS2366 fires when a function is declared to return a non-undefined type but has a code path that doesn't explicitly return a value. TypeScript detects that the function might implicitly return undefined.

Why This Happens

Your function's return type annotation doesn't include undefined or void, but there's a code path (usually a branch or missing final return) that doesn't return anything. TypeScript performs control flow analysis to detect this.

Code That Triggers TS2366

// TS2366 examples

function getLabel(status: "active" | "inactive"): string {
  if (status === "active") {
    return "Active User";
  }
  // Missing return for "inactive" branch
  // Error: Function lacks ending return statement and
  // return type does not include 'undefined'
}

function classify(score: number): "pass" | "fail" {
  if (score >= 60) return "pass";
  if (score < 60) return "fail";
  // TypeScript can't always prove all branches are covered
  // Error: Function lacks ending return statement
}

How to Fix TS2366

Option 1: Add a return for all code paths

function getLabel(status: "active" | "inactive"): string {
  if (status === "active") return "Active User";
  return "Inactive User"; // covers the else branch
}

Option 2: Add an exhaustive default / throw

function getLabel(status: "active" | "inactive"): string {
  switch (status) {
    case "active": return "Active User";
    case "inactive": return "Inactive User";
    default:
      const _exhaustive: never = status; // compile error if new case added
      throw new Error(`Unknown status: ${status}`);
  }
}

Option 3: Include undefined in the return type if appropriate

function getLabel(status: string): string | undefined {
  if (status === "active") return "Active User";
  // undefined is now allowed — caller must handle it
}

Frequently Asked Questions — TS2366

What does TS2366 mean?

TS2366 means TypeScript found a code path in your function that doesn't return a value, but the function's return type says it always returns something. Add a return statement for every branch or change the return type.

Why does TS2366 appear even when I handle all cases?

TypeScript's control flow analysis may not be able to prove all branches are covered — especially with number ranges. Add a final return or throw to make it exhaustive.

Should I add 'undefined' to the return type or add a return statement?

Prefer adding a return statement to cover the missing branch. Including undefined in the return type shifts the problem to callers who now must handle undefined. Only use it if the function legitimately might not return a value.

TS2366 with switch statements — how to fix?

Add a default case that either returns a fallback value or throws an error. The throw approach is preferred for exhaustive type checking — TypeScript will error if you add a new case to a discriminated union and forget to update the switch.

Does TS2366 appear with void return type?

No. Functions with return type void or undefined | void don't trigger TS2366 because implicit undefined returns are valid for those types.

Convert JavaScript to TypeScript automatically

Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.

Try the converter →

Other TypeScript Errors

From the blog

View all →