【问题标题】:Cannot invoke an expression whose type lacks a call signature TypeScript error无法调用类型缺少调用签名 TypeScript 错误的表达式
【发布时间】:2018-12-25 03:18:15
【问题描述】:

我无法解决类型兼容性的问题。问题出在:

this.options.list.push(...this.cloudData.map((e: Words) => [e.word, e.size] as[string, number]) as[string, number][]);

options: Options = {
  list: [] as ListEntry
};

ListEntry 是:

type ListEntry = [string, number];

错误是:

error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((...items: any[]) => number) | ((...items: [string, number][]) => number)' has no compatible call signatures.

有什么想法吗?


编辑

Words 类型:

export class Words {
  word: string;
  size: number;
}

【问题讨论】:

  • 这可能是因为 options.list 期望一个字符串作为它的第一个条目,但你给它一个数组。
  • 你能添加类型Words吗?
  • 现在添加了,抱歉
  • 什么是选项类型。您的代码主要对我有用,唯一的问题是列表字段应该是 list: [] as ListEntry[] typescriptlang.org/play/…
  • @TitianCernicova-Dragomir 感谢它的工作

标签: javascript typescript typescript-typings word-cloud


【解决方案1】:

已在 cmets 中解决,添加此问题不会一直无人回答:

list 的初始化已关闭。形成你的用法list应该是ListEntry的数组

let options = {
  list: [] as ListEntry[]
};

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2017-09-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2017-02-03
    • 2019-09-28
    相关资源
    最近更新 更多