【问题标题】:Argument of type 'X' is not assignable to parameter of type 'Y'“X”类型的参数不能分配给“Y”类型的参数
【发布时间】:2020-04-25 00:23:35
【问题描述】:

我已经创建了这些类型:

export type Optional<T> = T | null;

我有这个功能

updateBook( book: Array<Optional<Hostel>>)

我打电话的时候

 let book: Hostel | null [] = [null];
 updateBook(book)

但我有这个错误

 - error TS2345: Argument of type 'Hostel | null[]' is not assignable to parameter of type 'Optional<Hostel>[]

【问题讨论】:

    标签: javascript arrays node.js typescript


    【解决方案1】:

    Hostel | null [] 表示Hostelnull[] 之间的联合。 [] 的优先级高于联合运算符 (|)。

    您可能想说一个Hostel | null 的数组,可以写成(Hostel | null)[],或者更易读的IMO Array&lt;Hostel | null&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2021-04-09
      • 2021-03-02
      • 2016-04-16
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多