/** * null or undefined */ export type Nil = null | undefined; /** * A generic Class or constructor */ export type AnyConstructor = new (...args: any) => any; /** * A generic Predicate type */ export type Predicate = (x: any) => boolean; /** * A generic Struct interface */ export type AnyStruct = { [k in string | number | symbol]: Predicate | AnyStruct; }; /** * An object type with a given stringTag */ export type ObjWithStrTag = { [k in string | number | symbol]: any; } & { [Symbol.toStringTag]: U }; /** * Extract the guarded type from a type guard */ export type GuardedType = T extends (x: any) => x is infer T ? T : never; export type PredicatesToGuards = { [K in keyof T]: GuardedType }; /** * Get props that can be assigned U */ export type PickPropType = { [K in keyof T]: U extends T[K] ? K : never; }[keyof T]; /** * Get props that can be assigned undefined */ export type Undefinables = { [K in PickPropType]: T[K]; }; /** * Get props except those that can be assigned U */ export type ExcludePropType = { [K in keyof T]: U extends T[K] ? never : K; }[keyof T]; /** * Get props that cannot be assigned undefined */ export type Defined = { [K in ExcludePropType]: T[K]; }; type Id = {} & { [P in keyof T]: T[P] }; /** * Make props that can be undefined optional */ export type UndefinedOptional = Id & Partial>>; /** * Take an object of predicates and return * an object type containing the guarded types */ export type GuardedStruct = Struct extends (...x: any[]) => any ? GuardedType : UndefinedOptional< { [K in keyof Struct]: GuardedStruct; } >; /** * Add a member to the start of a tuple */ export type Unshift = (( front: FrontT, ...rest: TailT ) => any) extends (...tuple: infer TupleT) => any ? TupleT : never; /** * Create a tuple type of arbitrary length */ export type Tuple = { 0: OutputT; 1: Tuple>; }[OutputT["length"] extends LengthT ? 0 : 1]; /** * Convert a union to an intersection */ // How does this work? Blessed if I knew // https://stackoverflow.com/questions/50374908/transform-union-type-to-intersection-type/50375286#50375286 export type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ( k: infer I, ) => void ? I : never;