[英]Typescript: Argument of type X is not assignable to parameter of type Y & X. Type X is not assignable to type 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 []
表示Hostel
和null[]
之間的聯合。 []
的優先級高於聯合運算符 ( |
)。
你可能想說的是一系列Hostel | null
Hostel | null
可以寫為(Hostel | null)[]
或更易讀的 IMO Array<Hostel | null>
Array<Hostel | null>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.