Javascript 类型为'的参数;X';不可分配给类型为';Y';
我创建了这些类型:Javascript 类型为'的参数;X';不可分配给类型为';Y';,javascript,arrays,node.js,typescript,Javascript,Arrays,Node.js,Typescript,我创建了这些类型: export type Optional<T> = T | null; 但我有这个错误 - error TS2345: Argument of type 'Hostel | null[]' is not assignable to parameter of type 'Optional<Hostel>[] -错误TS2345:类型为“Hostel | null[]”的参数不能分配给类型为“Optional[]的参数 Hostel | null[]
export type Optional<T> = T | null;
但我有这个错误
- error TS2345: Argument of type 'Hostel | null[]' is not assignable to parameter of type 'Optional<Hostel>[]
-错误TS2345:类型为“Hostel | null[]”的参数不能分配给类型为“Optional[]的参数
Hostel | null[]
表示Hostel
和null[]之间的联合。[]
的优先级高于联合运算符(|
)
您可能想说一个Hostel | null
数组,它可以写成(Hostel | null)[
,或者更容易理解为IMO数组
let book: Hostel | null [] = [null];
updateBook(book)
- error TS2345: Argument of type 'Hostel | null[]' is not assignable to parameter of type 'Optional<Hostel>[]