/** * general utility types */ export type Override = Omit & Overrides; type FixArr = T extends readonly any[] ? Omit> : T; type DropInitDot = T extends `.${infer U}` ? U : T; type _DeepKeys = T extends object ? { [K in (string | number) & keyof T]: `${`${K}` extends `${number}` ? `[${K}]` : `.${K}`}${"" | _DeepKeys>}`; }[(string | number) & keyof T] : never; /** * recursively flattens a key from an arbitrary object * * @example * ```ts * type Keys = DeepKeys<{a: {b: Array<{c: unknown}>}}> * // ^? "a" | "a.b" | "a.b[${number}]" | "a.b[${number}].c" * ``` */ export type DeepKeys = DropInitDot<_DeepKeys>>; /** * make every property in an object optional _including_ properties of nested objects */ export type DeepPartial = T extends object ? { [k in keyof T]?: DeepPartial; } : T; /** * expands a type along its properties. This is useful as a performance hack when dealing with many intersections * * @example * ```typescript * type X = Expand<{foo: string} & {bar: string}> * ``` */ export type Expand = T extends infer O ? { [k in keyof O]: O[k]; } : never; /** * split a structured key into its head and tail for easier access * * @example * * type X = ConsKey<"[0].b.c"> * // ^? type X = ["0", "b.c"] */ type ConsKey = K extends `[${infer Index}]${infer Remaining}` ? Remaining extends `.${infer R2}` ? [Index, R2] : [Index, Remaining] : K extends `${infer Prop}.${infer Remaining}` ? Prop extends `${infer P2}[${infer R2}` ? [P2, `[${R2}.${Remaining}`] : [Prop, Remaining] : K extends `${infer Prop}[${infer Remaining}` ? [Prop, `[${Remaining}`] : [K, ""]; /** * extracts the type of a property along a structured key * * @example * ```typescript * type Test = { * a: { * b: { * c: 1 | 2 * } * } * }; * * type ExtractedKeyType = ExtractKey * // ^? type ExtractedKeyType = 1 | 2 * ``` * */ export type ExtractKey = K extends void ? T : NonNullable extends infer T_ ? ConsKey extends [infer Prop, infer Remaining] ? Prop extends keyof T_ ? Remaining extends "" ? T_ extends any[] ? T_[Prop] : Extract[Prop] | (undefined extends [...Path, T, T_[Prop]][number] ? undefined : never) : ExtractKey : never : never : never; /** * remove all keys from `From` where the value type is assignable to `ValueType`. * * This is useful to sort of "clean up" mapped types where mapped properties * should be omitted - e.g. when producing a record of `never`. * * @example * * ```ts * type Test = { a: never, b: string } * * type X = OmitValuetypes; * // ^? { b: string } * ``` */ export type OmitValuetypes = { [key in keyof From as From[key] extends ValueType ? never : key]: From[key]; } extends infer U ? U : never; /** * Represents a record with no keys. This should be used instead of `{}` since that basically means `any` */ export type EmptyRecord = Record; /** * Checks if a record has no keys (i.e. is empty) */ export type IsEmptyRecord = keyof T extends never ? true : false; /** * util type to transform a union into an intersection. * Works by placing the union constituents in a contravariant position and then re-inferring them * * @notice avoid usage unless you really know what you're doing */ export type UnionToIntersection = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never; type FilterKeysWithValue = { [k in keyof T]: T[k] extends V ? T[k] : R; }; type Primitives = string | number | boolean | null | undefined; /** * constrain a type to a certain subset of variants. Basically an `Extract` type on steroids. * * @example * ```typescript * type FormData = {a: number, b: {type: 1, foo: string} | {type: 2, bar: string}}; * type Constrained = DeepExtract * // ^? type Constrained = {a: number, b: {type: 1, foo: string}} * ``` */ export type DeepExtract = T extends FilterKeysWithValue, Primitives, any> ? { [k in keyof T]-?: k extends keyof U ? DeepExtract : T[k]; } : never; export {};