UNPKG

type-guard-pro

Version:

Zero-dependency TypeScript runtime type validation with full type inference

115 lines (112 loc) 5.19 kB
/** * Package version information * This file is automatically updated by the release process */ declare const VERSION: { current: string; name: string; description: string; repository: string; }; /** * Creates a type guard function for the specified type * @template T The type to create a guard for */ declare function createGuard<T>(): { /** * Creates a type guard for an object type * @param schema The object schema to validate against * @throws {ValidationError} When schema is invalid */ object: <S extends Record<string, unknown> & Partial<T>>(schema: { [K in keyof S]: (value: unknown) => value is S[K]; }) => (value: unknown) => value is S; /** * Creates a type guard for primitive types * @param validator Function to validate the primitive type */ primitive: <P>(validator: (value: unknown) => value is P) => (value: unknown) => value is P; /** * Creates a type guard for array types * @param itemGuard Type guard for array items */ array: <I>(itemGuard: (value: unknown) => value is I) => (value: unknown) => value is I[]; /** * Creates a type guard for union types * @param guards Array of type guards to check against */ union: <T1 extends T, T2 extends T>(guard1: (value: unknown) => value is T1, guard2: (value: unknown) => value is T2) => (value: unknown) => value is T1 | T2; /** * Creates a custom type guard with additional validation logic * @param validator Custom validation function */ custom: <C>(validator: (value: unknown) => value is C) => (value: unknown) => value is C; /** * Creates a type guard for intersection types * @param guards Array of type guards to check against */ intersection: <I_1>(...guards: ((value: unknown) => boolean)[]) => (value: unknown) => value is I_1; /** * Creates a guard for tuple types with specific element types * @param elementGuards Guards for each position in the tuple */ tuple: <T_1 extends unknown[]>(...elementGuards: { [K_1 in keyof T_1]: (value: unknown) => value is T_1[K_1]; }) => (value: unknown) => value is T_1; /** * Creates a guard for literal values * @param expectedValue The literal value to match */ literal: <L extends string | number | boolean | null | undefined>(expectedValue: L) => (value: unknown) => value is L; /** * Creates a guard for record types (objects with keys of type K and values of type V) * @param keyGuard Guard for the object keys * @param valueGuard Guard for the object values */ record: <K_2 extends string, V>(keyGuard: (key: string) => key is K_2, valueGuard: (value: unknown) => value is V) => (value: unknown) => value is Record<K_2, V>; /** * Helper for partial objects (all fields optional) * @param schema The object schema to validate against */ partial: <S_1 extends Record<string, unknown> & Partial<T>>(schema: { [K_3 in keyof S_1]: (value: unknown) => value is S_1[K_3]; }) => (value: unknown) => value is Partial<S_1>; /** * Creates a guard with runtime validation constraints * @param baseGuard The base type guard * @param validator Additional validation function with custom logic */ refined: <R>(baseGuard: (value: unknown) => value is R, validator: (value: R) => boolean) => (value: unknown) => value is R; }; declare const guards: { readonly string: (value: unknown) => value is string; readonly number: (value: unknown) => value is number; readonly boolean: (value: unknown) => value is boolean; readonly null: (value: unknown) => value is null; readonly undefined: (value: unknown) => value is undefined; readonly date: (value: unknown) => value is Date; readonly bigint: (value: unknown) => value is bigint; readonly symbol: (value: unknown) => value is symbol; readonly function: (value: unknown) => value is (...args: unknown[]) => unknown; readonly object: (value: unknown) => value is object; readonly integer: (value: unknown) => value is number; readonly positiveNumber: (value: unknown) => value is number; readonly negativeNumber: (value: unknown) => value is number; readonly nonEmptyString: (value: unknown) => value is string; readonly email: (value: unknown) => value is string; readonly url: (value: unknown) => value is string; readonly uuid: (value: unknown) => value is string; readonly iso8601Date: (value: unknown) => value is string; }; interface ValidationOptions { throwOnError?: boolean; strict?: boolean; } interface ErrorOptions { cause?: unknown; } declare class ValidationError extends Error { constructor(message: string, options?: ErrorOptions); } interface Plugin<T> { name: string; validate: (value: unknown) => value is T; } declare const plugins: Map<string, Plugin<unknown>>; declare function registerPlugin<T>(plugin: Plugin<T>): void; declare function getPlugin(name: string): Plugin<unknown> | undefined; export { type Plugin, VERSION, ValidationError, type ValidationOptions, createGuard, getPlugin, guards, plugins, registerPlugin };