Typescript 类型脚本长度未知的数组中所有对象的交集类型
我在Typescript中有一个对象数组的类型。比如说:Typescript 类型脚本长度未知的数组中所有对象的交集类型,typescript,types,Typescript,Types,我在Typescript中有一个对象数组的类型。比如说: type Arr = [{ toto: string}, {titi: number}]; 我事先不知道长度。 我想要数组中所有对象的合并类型,即交集 { toto: string, titi: number } 谢谢 正如您所发现的,您可以使用Arr[number]获得数组中所有类型的并集。然后,您可以使用所述的UnionToIntersection将其转换为交叉口: type Arr = [{ toto: string},
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}
非常好:)我试着写一些递归的东西。谢谢!非常好:)我正试着写一些递归的东西。谢谢!