/** Combines deduplicated arrays without any new duplicates */ type Combine = A extends [ ...infer S, infer L ] ? Equal, [...A, ...B]> : B; /** Checks whether two types are equal to each other */ type Equal = A extends B ? B extends A ? True : False : False; /** Checks whether the value is a tuple or an array */ type IsTuple = IsNever< T, False, T extends readonly [any, ...any] ? True : T extends [] ? True : False >; /** Checks whether the type `T` is equal to `never` */ type IsNever = [T] extends [never] ? True : False; /** Returns a flat type of any array/tuple/iterable */ type Flatten = IsTuple< T, T extends readonly [infer U, ...infer Tail extends any[]] ? U extends readonly any[] ? Flatten : U | Flatten : never, T extends string ? T : T extends Iterable ? U extends Iterable ? Flatten : U : T >; /** Returns the type of the last element of an array or tuple */ type Last = IsTuple extends true ? T extends [...any[], infer L] ? L : never : T extends (infer L)[] ? L : never; /** Function shorthand */ type Fn = (...args: A extends any[] ? A : [A]) => Z; /** Checks whether a tuple or an array contains the given type */ type Contains< T extends readonly any[], U, True = true, False = false > = IsTuple< T, T extends [infer Item, ...infer Rest] ? U extends Item ? True : Contains : False, T extends (infer Item)[] ? (U extends Item ? True : False) : never >; /** Converts any union to its intersection. Eg. `A | B` to `A & B` */ type Intersected = (U extends any ? (k: U) => void : never) extends ( k: infer I ) => void ? I : never; export type { Intersected, Contains, Combine, IsTuple, IsNever, Flatten, Equal, Last, Fn, };