Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Typescript 类型脚本长度未知的数组中所有对象的交集类型_Typescript_Types - Fatal编程技术网

Typescript 类型脚本长度未知的数组中所有对象的交集类型

Typescript 类型脚本长度未知的数组中所有对象的交集类型,typescript,types,Typescript,Types,我在Typescript中有一个对象数组的类型。比如说: type Arr = [{ toto: string}, {titi: number}]; 我事先不知道长度。 我想要数组中所有对象的合并类型,即交集 { toto: string, titi: number } 谢谢 正如您所发现的,您可以使用Arr[number]获得数组中所有类型的并集。然后,您可以使用所述的UnionToIntersection将其转换为交叉口: type Arr = [{ toto: string},

我在Typescript中有一个对象数组的类型。比如说:

type Arr = [{ toto: string}, {titi: number}];
我事先不知道长度。 我想要数组中所有对象的合并类型,即交集

{
  toto: string,
  titi: number
}

谢谢

正如您所发现的,您可以使用
Arr[number]
获得数组中所有类型的并集。然后,您可以使用所述的
UnionToIntersection
将其转换为交叉口:

type Arr = [{ toto: string}, {titi: number}];
type UnionToIntersection<U> = 
  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never

type All = UnionToIntersection<Arr[number]>
类型Arr=[{toto:string},{titi:number}];
类型UnionToIntersection=
(U扩展任何?(k:U)=>void:never)扩展((k:inferi)=>void)?I:从来没有
键入All=UnionToIntersection

正如您所发现的,您可以使用
Arr[number]
获得数组中所有类型的并集。然后,您可以使用所述的
UnionToIntersection
将其转换为交叉口:

type Arr = [{ toto: string}, {titi: number}];
type UnionToIntersection<U> = 
  (U extends any ? (k: U)=>void : never) extends ((k: infer I)=>void) ? I : never

type All = UnionToIntersection<Arr[number]>
类型Arr=[{toto:string},{titi:number}];
类型UnionToIntersection=
(U扩展任何?(k:U)=>void:never)扩展((k:inferi)=>void)?I:从来没有
键入All=UnionToIntersection

我试过
Arr[number]
但是这给了我们一个联合
{toto:string}{toto:number}
我试过
Arr[number]
但是这给了我们一个联合
{toto:string}{toto:number}
非常好:)我试着写一些递归的东西。谢谢!非常好:)我正试着写一些递归的东西。谢谢!