【发布时间】:2019-04-20 11:03:15
【问题描述】:
interface F1 {
(a, b): any;
}
interface F2 {
(a): any;
}
type F3 = F1 | F2;
const f: F3 = (a) => {
console.log(123, a);
}
f(1) // Error
我偶然发现了 TypeScript (3.1.4) 中的一个神秘问题。当我调用f() 时,编译器会显示Cannot invoke an expression whose type lacks a call signature. Type 'F3' has no compatible call signatures. [2349]。
这甚至很奇怪,因为在f(1) 之前,上述所有代码都可以正常工作。
我在这里遗漏了什么吗?如果有的话,我怎样才能给联合类型的函数打字?
我知道我可以做这样的事情
interface T {
(a, b): any;
(a): any;
}
但是我必须以这种方式定义函数
function (a, b?) {
}
我不太喜欢。任何帮助/反馈将不胜感激。
【问题讨论】:
标签: javascript typescript union overloading