为函数类型返回void

时间:2020-10-21 05:18:12

标签: typescript typescript-typings

我有一个明确定义的函数,它应该返回void。如果返回一些值,我会得到一个错误,这当然是正确的。

function test(): void {
  return true; // Correct error: Type 'boolean' is not assignable to type 'void'.
}

但是,如果我为一个函数定义了一个类型,该类型指出必须返回void并使用该类型,那么如果我从该函数返回一些值,我就不会得到任何错误。

type FuncType = () => void;

const testWithType: FuncType = () => {
  return true; // No error, but it should be.
};

Playground

为什么会有这种行为,如何修复我的类型才能使其正常工作?

0 个答案:

没有答案