代码之家  ›  专栏  ›  技术社区  ›  ffxsam

TypeScript忽略函数注释-当注释说应该返回void[duplicate]时返回字符串

  •  0
  • ffxsam  · 技术社区  · 6 年前

    function update(f: () => string) {...}
    function updateDeep(f: () => void) {...}
    

    如您所见,我试图确保我的API的客户端根据它们调用的函数传递正确的函数类型。

    update ,按预期工作。这将正确地抛出编译错误:

    update(() => console.log('hey'));
    

    第二个功能, updateDeep ,但不会引发编译错误事件,尽管它应该:

    updateDeep(() => 'hey');
    

    如何声明不返回任何内容的函数类型?

    0 回复  |  直到 7 年前
        1
  •  3
  •   Ryan Cavanaugh    7 年前

    这是不可能的。作为函数的接收者,您唯一的能力是设置 降低

    另请参见TypeScript常见问题: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void