diff --git a/is/factory.ts b/is/factory.ts index 138de9e..bc364e2 100644 --- a/is/factory.ts +++ b/is/factory.ts @@ -179,7 +179,18 @@ export function isTupleOf< } } -type TupleOf = { +/** + * Tuple type of types that are predicated by an array of predicate functions. + * + * ```ts + * import { is, TupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts"; + * + * type A = TupleOf; + * // Above is equivalent to the following type + * // type A = [string, number]; + * ``` + */ +export type TupleOf = { -readonly [P in keyof T]: T[P] extends Predicate ? U : never; }; @@ -314,8 +325,19 @@ export function isUniformTupleOf( ); } +/** + * Uniform tuple type of types that are predicated by a predicate function and the length is `N`. + * + * ```ts + * import { is, UniformTupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts"; + * + * type A = UniformTupleOf; + * // Above is equivalent to the following type + * // type A = [number, number, number, number, number]; + * ``` + */ // https://stackoverflow.com/a/71700658/1273406 -type UniformTupleOf< +export type UniformTupleOf< T, N extends number, R extends readonly T[] = [], @@ -536,18 +558,30 @@ function isWithOptional>( return isOptional(pred) || (pred as any).optional === true; } -type ObjectOf>> = FlatType< - // Non optional - & { - [K in keyof T as T[K] extends WithOptional ? never : K]: T[K] extends - Predicate ? U : never; - } - // Optional - & { - [K in keyof T as T[K] extends WithOptional ? K : never]?: T[K] extends - Predicate ? U : never; - } ->; +/** + * Object types that are predicated by predicate functions in the object `T`. + * + * ```ts + * import { is, ObjectOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts"; + * + * type A = ObjectOf<{ a: typeof is.Number, b: typeof is.String }>; + * // Above is equivalent to the following type + * // type A = { a: number; b: string }; + * ``` + */ +export type ObjectOf>> = + FlatType< + // Non optional + & { + [K in keyof T as T[K] extends WithOptional ? never : K]: T[K] extends + Predicate ? U : never; + } + // Optional + & { + [K in keyof T as T[K] extends WithOptional ? K : never]?: T[K] extends + Predicate ? U : never; + } + >; type IsObjectOfMetadata = { name: "isObjectOf";