From 54ad8858a227484b1a1d93cee7c390f355f24964 Mon Sep 17 00:00:00 2001 From: kyoh86 Date: Tue, 13 Feb 2024 04:13:18 +0900 Subject: [PATCH] re-export ObjectOf, TupleOf and UniformTupleOf types --- is/factory.ts | 62 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 14 deletions(-) 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";