![](/img/trans.png)
[英]How to define the return type of a function according to the input parameters with Typescript?
[英]How to define a function which cannot return anything in TypeScript
我正在编写一个具有2个非常相似的函数的API:
function update(f: () => string) {...}
function updateDeep(f: () => void) {...}
如您所见,我正在尝试确保API的客户端根据调用的函数传递正确的函数类型。
第一个功能update
可以按预期工作。 这将正确地引发编译错误:
update(() => console.log('hey'));
第二个函数updateDeep
不会抛出编译错误事件,尽管它应该:
updateDeep(() => 'hey');
如何声明不返回任何内容的函数类型?
不可能做到这一点。 作为函数的接收者,您唯一的能力是为所提供的函数设置下限 。
另请参阅TypeScript常见问题解答条目: https : //github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.