import type { UnionToIntersection, UnionToTuple } from "./union.ts" // ============================================================================ // Generation // ============================================================================ /** * @description Create an intersection from a tuple of types. * * @example * ``` * // Expect: { a: 1 } & { b: 2 } * type Example1 = IntersectionAll<[{ a: 1 }, { b: 2 }]> * // Expect: string & number * type Example2 = IntersectionAll<[string, number]> * // Expect: unknown * type Example3 = IntersectionAll<[]> * ``` */ export type IntersectionAll = [ T[number], ] extends [never] ? Fallback : UnionToIntersection // ============================================================================ // Manipulation // ============================================================================ /** * @description Merge an intersection of object types into a single object type. * * @example * ``` * // Expect: { a: 1; b: 2 } * type Example1 = IntersectionMerge<{ a: 1 } & { b: 2 }> * // Expect: { a: string & number } * type Example2 = IntersectionMerge<{ a: string } & { a: number }> * // Expect: {} * type Example3 = IntersectionMerge<{}> * ``` */ export type IntersectionMerge> = { [Key in keyof T]: T[Key] } /** * @description Prettify an intersection of object types. * * @example * ``` * // Expect: { id: string; active: boolean } * type Example1 = IntersectionPrettify<{ id: string } & { active: boolean }> * // Expect: { value: number } * type Example2 = IntersectionPrettify<{ value: number }> * // Expect: {} * type Example3 = IntersectionPrettify<{}> * ``` */ export type IntersectionPrettify> = IntersectionMerge // ============================================================================ // Conversion // ============================================================================ /** * @description Convert an intersection of object types into a union of object types. * * @example * ``` * // Expect: { a: string } | { b: number } * type Example1 = ObjectIntersectionToUnion<{ a: string } & { b: number }> * // Expect: { a: never } * type Example2 = ObjectIntersectionToUnion<{ a: string } & { a: number }> * // Expect: never * type Example3 = ObjectIntersectionToUnion<{}> * ``` */ export type ObjectIntersectionToUnion = { [K in keyof T]: { [P in K]: T[P] } }[keyof T] /** * @description Convert an intersection of object types into a tuple of object types. * * @example * ``` * // Expect: [{ a: string }, { b: number }] * type Example1 = ObjectIntersectionToTuple<{ a: string } & { b: number }> * // Expect: [{ a: never }] * type Example2 = ObjectIntersectionToTuple<{ a: string } & { a: number }> * // Expect: [] * type Example3 = ObjectIntersectionToTuple<{}> * ``` */ export type ObjectIntersectionToTuple = UnionToTuple>