Fix Not All Code Paths Return a Value in TypeScript
Fix TypeScript errors TS7030 and TS2366, not all code paths return a value, with a final return, an explicit undefined, a throw or an exhaustive switch.

Two related errors report a function that sometimes falls off its end without returning. TS7030, "Not all code paths return a value", comes from the noImplicitReturns option. TS2366, "Function lacks ending return statement and return type does not include 'undefined'", appears whenever a function with a declared return type can reach its closing brace. Both mean the same thing for your program: on some input, the function returns undefined, and the type says it will not. This post covers the five causes in TypeScript 6.x and the fix for each. The behavior is the same in the native TypeScript 7 compiler. The option is documented in the TSConfig reference.
The two errors
function sign(n: number): string {
if (n > 0) {
return "positive";
}
if (n < 0) {
return "negative";
}
}missing.ts(1,23): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.When n is 0, both conditions fail, the function ends, and the caller gets undefined where it expected a string. TS2366 is reported under any configuration when a return type is declared. TS7030 applies when some paths return a value and others use a bare return; or fall off the end in a function whose return type is inferred or includes undefined.
function find(items: string[], target: string) {
for (const item of items) {
if (item === target) {
return item;
}
}
}implicit.ts(1,10): error TS7030: Not all code paths return a value.With noImplicitReturns on, even an inferred string | undefined return is flagged, because the missing path is probably not deliberate. The option is not part of strict, and we recommend enabling it.
Fix 1: return a value on every path
If the function should produce a result for every input, add the missing case.
function sign(n: number): string {
if (n > 0) {
return "positive";
}
if (n < 0) {
return "negative";
}
return "zero";
}
console.log(sign(5), sign(-2), sign(0));positive negative zeroOften the cleaner form is a chain with a final fallback, or an early return for the unusual case followed by the main path. Whichever you pick, the last statement in the function should be a return or a throw.
Fix 2: say that the result may be missing
If "not found" is a legitimate outcome, put it in the type and return it explicitly. This is the honest fix for find.
function find(items: string[], target: string): string | undefined {
for (const item of items) {
if (item === target) {
return item;
}
}
return undefined;
}
console.log(find(["a", "b"], "b"), find(["a", "b"], "z"));b undefinedThe explicit return undefined satisfies noImplicitReturns and tells the reader that the end of the loop is a deliberate outcome. Callers must now handle the undefined, which is the point. If a thrown error is a better signal than a missing value, use that, as in the next fix.
Fix 3: throw when the end is unreachable by design
When the input cannot reach the end unless something is wrong, throw there. TypeScript sees the throw as ending the path, so no value is needed.
type Shape = { kind: "circle"; radius: number } | { kind: "square"; side: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side ** 2;
default:
throw new Error(`Unknown shape: ${JSON.stringify(shape)}`);
}
}
console.log(area({ kind: "square", side: 3 }));9Inside default, shape has type never, since both kinds were handled. The throw covers inputs that bypass the types, such as data from JSON, and it catches a new kind added later if the type is not updated.
Fix 4: make the switch exhaustive and let the compiler check it
For a union, you can omit the default and let the compiler prove that every case returns. If a case is missing, the function fails to compile.
type Shape = { kind: "circle"; radius: number } | { kind: "square"; side: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side ** 2;
}
}
console.log(area({ kind: "circle", radius: 1 }).toFixed(2));3.14This compiles because the compiler knows shape.kind is either "circle" or "square", so no path reaches the end. If you add { kind: "triangle" } to the union and forget the case, the error TS2366 returns, which is the safety net you want. An assertion helper makes the intent explicit and also protects at runtime.
function assertNever(value: never): never {
throw new Error(`Unexpected value: ${JSON.stringify(value)}`);
}
type Light = "red" | "yellow" | "green";
function next(light: Light): Light {
switch (light) {
case "red":
return "green";
case "green":
return "yellow";
case "yellow":
return "red";
default:
return assertNever(light);
}
}
console.log(next("red"));greenAdding a fourth light without a case makes assertNever(light) a compile error, since light is no longer never.
Fix 5: handle the callback and async cases
Callbacks passed to map, filter and reduce follow the same rule. A map callback that returns in an if and not otherwise produces undefined entries.
const numbers = [1, 2, 3, 4];
const labels = numbers.map((n) => {
if (n % 2 === 0) {
return "even";
}
});
console.log(labels);[ undefined, 'even', undefined, 'even' ]With noImplicitReturns the callback is flagged. Add the other branch, or use filter first if you meant to drop items. In async functions, the same applies to the promise's value: an async function that returns in one branch resolves to undefined in the other, and the return type Promise<string> rejects that at compile time.
What counts as a return
The compiler follows control flow, so several forms satisfy it. A return ends the path. A throw ends it. A call to a function whose return type is never, such as process.exit(), ends it, provided the function was declared with an explicit type. An infinite loop, such as while (true) without a break, ends the path since the end is unreachable. A try with a finally follows the same rules for each part, which is a source of surprises: a return in finally overrides the others.
Functions returning void or any are exempt from TS7030, and an explicit return; in a void function is fine.
Why falling off the end is a bug magnet
A function with no return on some path gives undefined, and JavaScript does not complain. The caller then does something with the result: reads a property, passes it on, formats it into a message. The failure shows up one or two calls away, as "Cannot read properties of undefined" or as the literal text "undefined" in a page. Because the cause is a missing statement, there is nothing in the stack trace to point at it. The compiler check moves the discovery to the function that is missing the return, which is the cheapest place to fix it.
The error is especially valuable after a refactor. Adding an if around a block, extracting part of a function, or changing a condition can make a path that used to return skip its return. With the check on, the compiler reports that function immediately, instead of waiting for a test to hit the case.
Choosing between undefined, null and throwing
When a function has a legitimate "no result" outcome, three styles are common. Returning undefined matches the standard library, as in Array.prototype.find, and works well with optional chaining and ??. Returning null marks an intentional absence, and some APIs use it for "looked and found nothing", as document.querySelector does. Throwing is for cases where the caller cannot sensibly continue, such as a missing required configuration value.
Pick one style per module and keep to it. A codebase where some lookups return undefined, others null, and others throw makes every caller check three things. If the absence is part of normal operation, return it. If it means a bug or a broken environment, throw, with a message that names what was missing. Either way, write the choice in the return type or in a short comment, so callers know before they read the body.
Edge cases the compiler handles
A few constructs behave in ways worth knowing. A while (true) loop with no break has no reachable end, so a function that returns inside it needs nothing after it. A function whose last statement is a call to another function declared as returning never is also complete. A switch with a default that returns or throws is complete, and a switch over a union without a default is complete only if every member is handled. The compiler also tracks narrowing: after if (typeof x === "string") return a; if (typeof x === "number") return b; on a string | number parameter, the end is unreachable, because x is never there.
Finally, remember overloads and abstract methods. An abstract method has no body, so it needs no return. An overload signature declares a type only. The implementation signature is the one the compiler checks for complete returns.


