并非所有代码路径都返回 TypeScript 中的值

并非所有代码路径都在 TypeScript 中返回值

Not all code paths return a value in TypeScript

当函数中的某些代码路径不返回值时,会出现错误“并非所有代码路径都返回值”。要解决该错误,请确保从函数中的所有代码路径返回一个值或在您的文件中设置noImplicitReturns
falsetsconfig.json

并非所有代码路径都返回值

以下是错误发生方式的 2 个示例。

索引.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; }

在第一个示例中,我们将一个函数传递给Array.map()方法,但只有部分函数的路径返回值。

例如,如果不满足条件,函数将隐式返回
undefined

如果您只想消除错误,请在您的文件中设置noImplicitReturns为。falsetsconfig.json

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

现在一个函数能够像上面的例子一样隐式返回一个值。

但是,如果要修复错误,则必须确保该函数在所有代码路径中显式返回一个值。

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

如果满足回调函数中的条件,则返回element * 2,否则返回element * 3,因此该函数始终保证返回 a
number

所有代码路径都必须显式返回一个值:

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

上面的函数不可能不返回 type 的值number,因为我们已经涵盖了所有代码路径。

您的函数中很可能会有更多嵌套条件。开始调试的一种方法是显式设置函数的返回类型。

索引.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 #

当函数中的某些代码路径不返回值时,会出现错误“并非所有代码路径都返回值”。要解决该错误,请确保从函数中的所有代码路径返回一个值或在您的文件中设置noImplicitReturns
falsetsconfig.json