• Error
  • Reported by the type checker

TS2366

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

A function with a return type can reach its end without returning. Add a final `return` or `throw`, or include `undefined` in the return type.

What it means

When a function declares a return type that doesn't include undefined, every path through it must return or throw. A path that falls off the end, such as an if without an else, is reported at the return type.

Example

index.ts
export function sign(n: number): string {
  if (n > 0) {
    return "positive";
  } else if (n < 0) {
    return "negative";
  }
}
ts-r-check -p tsconfig.json --pretty false ts-r output
index.ts(1,34): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.

How to fix

  • Return or throw on the remaining path.
  • If returning nothing is intended, add | undefined to the return type.