将联合类型转换为相交类型

有没有一种方法可以将联合类型转换为交叉类型:

type FunctionUnion = () => void | (p: string) => void
type FunctionIntersection = () => void & (p: string) => void

我想申请一个转型FunctionUnion来获得FunctionIntersection

humengqiao2021/07/14 21:43:41

type FunctionIntersection<T extends (arg?: any) => void> = (T extends (arg?: any) => void ? (arg: T) => void : never) extends ((arg: infer P) => void) ? P : never

 

type T = FunctionIntersection<FunctionUnion>

 

T为(() => void) & ((p: string) => void)交叉类型

 

上面打错字了!

humengqiao2021/07/14 21:42:22

type FunctionIntersection<T extends (arg?: any) => void> = (T extends (arg?: any) => void ? (arg: T) => void : never) extends ((arg: infer P) => void) ? P : never

 

type T = FunctionIntersection<FunctionUnion>

 

T为(() => void) & ((p: string) => void)联合类型