Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript says a function lacks an ending return statement

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

Short answer

TypeScript found a way for your function to reach its closing brace without returning a value, while the declared return type does not allow undefined. Find that path: a missing return in an else branch, a switch that does not cover every case, or a return inside forEach. Return a value there, or widen the type.

  • Applies to TypeScript 5.x, React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Let the compiler point at the function, then read it from top to bottom and ask which path ends without a return. The error underlines the return type, not the missing line.

    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1An if or else branch, a try or a catch ends without returnOne branch never returnsFix
    2. 2The function switches on a union type and the error appears after you add a new memberA switch does not cover every caseFix
    3. 3return appears only inside arr.forEach(...) in the middle of the functionThe return sits inside a forEach callbackFix
    4. 4strictNullChecks is on and the function can legitimately find nothingundefined is a real possible result, but the type says it is notFix

What does the error look like?

  • error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
  • A function with a declared return type, such as Promise<string>, shows the red line under the type

Cause 1 of 4

One branch never returns

1Check

The TypeScript docs show the typical case: an else branch that evaluates a value but forgets the return keyword, so the function can finish with nothing returned.

npx tsc --noEmit --noImplicitReturns

2Fix

Return a value on every path.

function pickLabel(color: 'blue' | 'black'): string {
  if (color === 'blue') {
    return 'beats';
  } else {
    return 'bose'; // the missing keyword
  }
}

3Verify

Run the type check again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 4

A switch does not cover every case

1Check

If a new member is added to a union and the switch has no branch for it, execution falls out of the switch with no return.

grep -n "switch" src/utils/*.ts

2Fix

Add a default that assigns the value to a never-typed constant. When every case is handled it compiles, and when a case is missing it fails and names the leftover type.

function area(shape: Shape): number {
  switch (shape.kind) {
    case 'circle':
      return Math.PI * shape.radius ** 2;
    case 'square':
      return shape.sideLength ** 2;
    default: {
      const _exhaustive: never = shape;
      return _exhaustive;
    }
  }
}

3Verify

Add a new member to the union on purpose and confirm the compiler now points at this switch.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 4

The return sits inside a forEach callback

1Check

forEach always returns undefined and discards the value the callback returns, so a return inside it ends only that call. The outer function still reaches its closing brace without a value.

grep -n "forEach" src/api/*.ts

2Fix

Use a for...of loop, or find, some or every, when you need to leave the outer function early.

function findUser(users: User[], id: string): User | undefined {
  for (const u of users) {
    if (u.id === id) return u;
  }
  return undefined;
}

3Verify

Type-check, then run the function with an id that exists and one that does not.

npx tsc --noEmit

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

undefined is a real possible result, but the type says it is not

1Check

With strictNullChecks true, null and undefined have their own types, so a function that may find nothing must say so.

grep -n "strict" tsconfig.json

2Fix

Include undefined in the return type and return it, or throw when a missing value is an error.

async function loadName(id: string): Promise<string | undefined> {
  const user = await getUser(id);
  if (!user) return undefined;
  return user.name;
}

3Verify

Callers now get a type error until they handle undefined, which is the point.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Turn on noImplicitReturns so a path without a return is reported even when the return type allows undefined.
  • Use a never check in every switch over a union.
  • Prefer for...of over forEach when the loop must return something.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript says a JavaScript module implicitly has an any type

Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.

TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.

2 causes, each with a fix

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.