【问题标题】:error TS2349: Cannot invoke an expression whose type lacks a call signature错误 TS2349:无法调用其类型缺少调用签名的表达式
【发布时间】:2023-03-20 07:50:01
【问题描述】:

我正在使用 Angular 2 和 TypeScript 2。

当我使用时

let labels: string[] | number[] = [];
// let labels: Array<number> | Array<string> = [];

labels.push(1);

它给了我错误:

错误 TS2349:无法调用其类型缺少调用的表达式 签名。

【问题讨论】:

    标签: angular typescript typescript2.0


    【解决方案1】:

    变化

    let labels: string[] | number[] = [];
    

    到这些中的任何一个

    let labels: (string | number)[] = [];
    let labels: Array<number|string> = [];
    

    会解决问题。

    【讨论】:

    • 我意识到这是旧的,但我想我还是会在这里问...我不认为string[] | number[] 等同于(string | number)[]。我认为第一个选项意味着labels 将是一个 all 字符串数组或 all 数字数组。而第二个选项意味着labels 是一个数组,其内容可以是字符串或数字,可以互换。对此有什么想法吗?
    • @austinbruch 是的,你是对的。这里有更多信息:github.com/Microsoft/TypeScript/issues/10620。在该链接中,使用filter 的代码也发生了同样的问题,因为我想filter 在幕后使用pushpush 不起作用是有道理的,因为:如果 labelsstring[] 而不是 number[],那么这段代码显然不起作用:labels.push(1)
    猜你喜欢
    • 2020-02-07
    • 2020-05-17
    • 2016-03-30
    • 2017-10-11
    • 2017-02-03
    • 2017-12-19
    • 2017-07-14
    • 2020-01-29
    • 2019-09-10
    相关资源
    最近更新 更多