// ================================ // Helper types for making custom typescript errors // ================================ const _errorSymbol = Symbol() export type TypeError = TMessage & { _: typeof _errorSymbol } // Helper type to remove TypeErrors from unions /** * Remove TypeErrors from a union to allow the actual type to be used. */ export type ExcludeTE = Exclude> /** * Type assertion helper that removes TypeErrors from a union type. * This is a no-op function that only exists to help TypeScript's type system. * Use this when ExcludeTE type helper alone isn't sufficient to narrow the type. */ export function ASSERT_NO_TYPE_ERROR(_: T): asserts _ is ExcludeTE {} /** * Type assertion helper that removes TypeErrors from a union type. * This is a unit function that returns it's input and only exists to help TypeScript's type system. */ export function EXCLUDE_TYPE_ERROR(_: T): ExcludeTE { return _ as ExcludeTE } // ================================ // String type manipulation // ================================ export declare type UnionToIntersection = ( U extends any ? (k: U) => void : never ) extends (k: infer I) => void ? I : never export type UnionToOvlds = UnionToIntersection< U extends any ? (f: U) => void : never > export type PopUnion = UnionToOvlds extends (a: infer A) => void ? A : never export type StringConcatenationOrder< S extends string, SEPARATOR extends string, > = PopUnion extends infer SELF ? // SELF extends string ? Exclude extends never ? `${SELF}` : // This works because the values of S are always interpreted in ascending lexiographical order `${StringConcatenationOrder, SEPARATOR>}${SEPARATOR}${SELF}` : never : never