[英]Typescript return value or curried function
Given the following Typescript code, I get an error 给出以下打字稿代码,我得到一个错误
TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'AddReturnType' has no compatible call signatures.
Why doesn't AddReturnType
work with that call? 为什么
AddReturnType
不能与该调用一起使用?
type AddReturnType = number | ((arg0: number) => number);
function add(x: number, y?: number) : AddReturnType {
if (!y) {
return (val) => val + y;
}
return x + y;
}
add(1)(2);
TypeScript can't determine whether you're returning a number
or function, and only one of the two options is callable. TypeScript不能确定您要返回
number
还是函数,并且只能调用两个选项之一。 Make the two function signatures separate: 将两个函数签名分开:
function add(x: number): (number) => number;
function add(x: number, y: number): number;
function add(x, y?) {
if (!y) {
return (val) => val + y;
}
return x + y;
}
add(1)(2);
That said, shouldn't it be val + x
? 也就是说,不是
val + x
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.