【问题标题】:Typescript function union type lacks a call signatureTypescript 函数联合类型缺少调用签名
【发布时间】: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


    【解决方案1】:

    在打字稿中,| 运算符描述了一个union type

    联合类型描述的值可以是多种类型之一。我们使用竖线 (|) 来分隔每种类型,所以 number |字符串 | boolean 是值的类型,可以是数字、字符串或布尔值。

    interface F1 {
      (a, b): any;
    }
    
    interface F2 {
      (a): any;
    }
    
    type F3 = F1 | F2;
    const f: F3 = (a) => {
      console.log(123, a);
    }
    
    const isF2 = (a): a is F2 => true; // Typeguard
    if (isF2(f)) { 
      f(1); // OK
    }
    

    您正在寻找的是& 运算符,或intersection type

    交集类型表示同时具有多种类型的值。交集类型 A & B 的值是类型 A 和类型 B 的值。

    interface F1 {
      (a, b): any;
    }
    
    interface F2 {
      (a): any;
    }
    
    type F4 = F1 & F2;
    const g: F4 = (a) => {
      console.log(123, a);
    }
    
    g(1); // OK
    

    您可以在Typescript playground 中尝试这些示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2020-02-25
      • 2019-02-08
      • 2019-09-28
      相关资源
      最近更新 更多