TypeScript says a function lacks an ending return statement
error 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.
Fix it fast
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 --noEmit2Match what you see, then jump to its fix
- 1
An if or else branch, a try or a catch ends without returnOne branch never returnsFix - 2
The function switches on a union type and the error appears after you add a new memberA switch does not cover every caseFix - 3
return appears only inside arr.forEach(...) in the middle of the functionThe return sits inside a forEach callbackFix - 4
strictNullChecks is on and the function can legitimately find nothingundefined is a real possible result, but the type says it is notFix
- 1
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 --noImplicitReturns2Fix
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 --noEmitConfirmed by typescriptlang.org
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/*.ts2Fix
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 --noEmitConfirmed by typescriptlang.org
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/*.ts2Fix
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 --noEmitConfirmed by developer.mozilla.org
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.json2Fix
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 --noEmitConfirmed by typescriptlang.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixWeb apps
TypeScript 2695: left side of comma operator is unused in React code
Left side of comma operator is unused and has no side effects.ts(2695)
In JavaScript a comma between expressions evaluates all of them and keeps only the last, so the left ones are thrown away.
3 causes, each with a fixWeb 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 fixWeb apps
TypeScript: property does not exist on type IntrinsicAttributes in a React component
Object literal may only specify known properties
TypeScript checks the props you write in JSX against the component's props type.
3 causes, each with a fixWeb 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