Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类型为'的参数;X';不可分配给类型为';Y';_Javascript_Arrays_Node.js_Typescript - Fatal编程技术网

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