簡體   English   中英

“X”類型的參數不能分配給“Y”類型的參數

[英]Argument of type 'X' is not assignable to parameter of type 'Y'

我創建了這些類型:

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>[]

Hostel | null [] Hostel | null []表示Hostelnull[]之間的聯合。 []的優先級高於聯合運算符 ( | )。

你可能想說的是一系列Hostel | null Hostel | null可以寫為(Hostel | null)[]或更易讀的 IMO Array<Hostel | null> Array<Hostel | null>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM