Not all code paths return a value in TypeScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Not all code paths return a value in TypeScript #

The error "Not all code paths return a value" occurs when some of the code paths in a function don't return a value. To solve the error, make sure to return a value from all code paths in the function or set noImplicitReturns to false in your tsconfig.json file.

not all code paths return value

Here are 2 examples of how the error occurs.

index.ts
// ⛔️ Error: Not all code paths return a value.ts(7030) const result = [1, 2, 3].map((element) => { if (Math.random() > 0.5) { return element * 2; } // 👉️ Missing return statement here }); // ----------------------------------------- function doMath() { const nums = [1, 2, 3, 4, 5, 6]; // ⛔️ Not all code paths return a value.ts(7030) nums.forEach((num) => { if (num === 0) { return num; } if (num % 2) { return num * 2; } // 👉️ returning from forEach callback is a mistake }); return nums; }

In the first example, we pass a function to the Array.map() method, but only some of the function's paths return a value.

For example, if the condition is not met, the function implicitly returns undefined.

If you just want to silence the error, set noImplicitReturns to false in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { "noImplicitReturns": false, } }

Now a function is able to implicitly return a value like in the examples above.

However, if you want to fix the error, you have to make sure that the function explicitly returns a value in all of the code paths.

index.ts
const result = [1, 2, 3].map((element) => { if (Math.random() > 0.5) { return element * 2; } return element * 3; });

If the condition in the callback function is met, we return element * 2, else, we return element * 3, so the function is always guaranteed to return a number.

All code paths have to explicitly return a value:

index.ts
function example() { if (Math.random() > 0.5) { return 100; } else if (Math.random() > 0.4) { return 150; } return 200; }

There is no way the function above does not return a value of type number, as we have covered all code paths.

You will most likely have more nested conditionals in your function. One way to start debugging is to explicitly set the return type of the function.

index.ts
// 👇️ set return type of function to `number` // right after function's parameters const result = [1, 2, 3].map((element): number => { if (Math.random() > 0.5) { return element * 2; } return element * 3; });

We set the function's return type to number, so if we don't explicitly return a value of number type in all of the function's call paths, TypeScript will alert us that we are returning undefined, which is not included in the function's return type.

Note that when you return from a nested function, you don't actually return from the outer function.

The error commonly occurs when returning from a nested function like the ones we pass to Array.forEach().

index.ts
function doMath() { const nums = [1, 2, 3, 4, 5, 6]; // ⛔️ Not all code paths return a value.ts(7030) nums.forEach((num) => { if (num === 0) { return num; } if (num % 2) { return num * 2; } }); return nums; }

When we return from a nested function, like the one we passed to the forEach() method, we do not return from the enclosing function (doMath in the example).

Returning from the forEach() method exits the current iteration and moves to the next iteration. It's the same as using a continue statement in afor loop.

The doMath function in the example above returns the nums variable a 100% of the time.

To solve the error in the example, we have to remove the return statements from function we passed to the forEach() method.

If you use the return statements as continue statements, you have to add a return statement for all code paths.

index.ts
function doMath() { const nums = [1, 2, 3, 4, 5, 6]; const result: number[] = []; nums.forEach((num) => { if (num % 2) { result.push(num * 2); } else { result.push(num * 3); } }); return nums; }

In this situation, we have to either use the return statement in all code paths or simply remove all return statements from the callback function.

Conclusion #

The error "Not all code paths return a value" occurs when some of the code paths in a function don't return a value. To solve the error, make sure to return a value from all code paths in the function or set noImplicitReturns to false in your tsconfig.json file.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.