ts-type
Version:
TypeScript 類型工具庫:提供豐富的類型操作工具和重新導出的內建類型 / TypeScript type utility library: provides rich type manipulation utilities and re-exported built-in types
13 lines (12 loc) • 619 B
TypeScript
/**
* 將聯集類型轉換為交集類型
* Convert union type to intersection type
*
* @see https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type
* @example
* type FunctionUnion = (() => void) | ((p: string) => void);
* type FunctionIntersection = (() => void) & ((p: string) => void);
* type SynthesizedFunctionIntersection = ITSUnionToIntersection<FunctionUnion>
* // type SynthesizedFunctionIntersection = (() => void) & ((p: string) => void)
*/
export type ITSUnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;