简体   繁体   English

“X”类型的参数不能分配给“Y”类型的参数

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

I have created those types:我创建了这些类型:

export type Optional<T> = T | null;

and I have this function我有这个功能

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

that I call with我打电话给

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

but I have this error但我有这个错误

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

Hostel | null [] Hostel | null [] means a union between Hostel and null[] . Hostel | null []表示Hostelnull[]之间的联合。 The [] has a higher precedence than the union operator ( | ). []的优先级高于联合运算符 ( | )。

You probably wanted to say an array of Hostel | null你可能想说的是一系列Hostel | null Hostel | null which can be written as (Hostel | null)[] or, more readably IMO Array<Hostel | null> Hostel | null可以写为(Hostel | null)[]或更易读的 IMO Array<Hostel | null> Array<Hostel | null> . Array<Hostel | null>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:X 类型的参数不能分配给 Y 和 X 类型的参数。X 类型不能分配给 Y 类型 - Typescript: Argument of type X is not assignable to parameter of type Y & X. Type X is not assignable to type Y TYPESCRIPT :类型“typeof X”的参数不能分配给类型为“Y”的带有扩展的参数 - TYPESCRIPT : Argument of type 'typeof X' is not assignable to parameter of type 'Y' with extends NGRX 减速器问题:“X”类型的参数不能分配给“Y”类型的参数 - NGRX reducer problem : Argument of type "X" is not assignable to parameter of type "Y" “x”类型的参数不能分配给 Angular7 中“x”类型的参数 - Argument of type 'x' is not assignable to parameter of type 'x' in Angular7 x 类型的参数不能分配给 '{ x: any; 类型的参数。 }' - Argument of type x is not assignable to parameter of type '{ x: any; }' “Ref”类型的参数<x> ' 不可分配给 'x' 类型的参数</x> - Argument of type 'Ref<x>' is not assignable to parameter of type 'x' 参数类型不能分配给类型的参数 - Argument type is not assignable to parameter of type &#39;number&#39; 类型的参数不能分配给类型 &#39;{ [x: number] }&#39; 的参数 - Argument of type 'number' is not assignable to parameter of type '{ [x: number] }' React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type [type]类型的参数不能分配给[type]类型的参数 - Argument of type [type] is not assignable to parameter of type [type]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM