/** * @file Runtime Type Checker * @description Production-grade runtime type checking with type guards, * assertion functions, branded types, and narrowing utilities. * * Features: * - Type guard functions for runtime verification * - Assertion functions with detailed error messages * - Branded types for nominal typing * - Type narrowing utilities * - Composable checkers * * @example * ```typescript * import { is, assert, brand } from '@/lib/data/validation'; * * // Type guards * if (is.string(value)) { * value.toUpperCase(); // TypeScript knows it's a string * } * * // Assertions * assert.string(value, 'Expected string'); * value.toUpperCase(); // TypeScript knows it's a string * * // Branded types * type UserId = Brand; * const userId = brand(id); * ``` */ /** * Brand type for nominal typing * Prevents accidental assignment between structurally identical types */ declare const BRAND: unique symbol; export type Brand = T & { [BRAND]: B; }; /** * Create a branded value */ export declare function brand>(value: T extends Brand ? U : never): T; /** * Extract the base type from a branded type */ export type Unbrand = T extends Brand ? U : T; /** * Type guard function signature */ export type TypeGuard = (value: unknown) => value is T; /** * Type assertion function signature */ export type TypeAssertion = (value: unknown, message?: string) => asserts value is T; /** * Runtime check result */ export interface CheckResult { success: boolean; value: T | undefined; error?: string; } /** * Check if value is a string */ export declare function isString(value: unknown): value is string; /** * Check if value is a number (excludes NaN) */ export declare function isNumber(value: unknown): value is number; /** * Check if value is a boolean */ export declare function isBoolean(value: unknown): value is boolean; /** * Check if value is a bigint */ export declare function isBigInt(value: unknown): value is bigint; /** * Check if value is a symbol */ export declare function isSymbol(value: unknown): value is symbol; /** * Check if value is undefined */ export declare function isUndefined(value: unknown): value is undefined; /** * Check if value is null */ export declare function isNull(value: unknown): value is null; /** * Check if value is null or undefined */ export declare function isNullish(value: unknown): value is null | undefined; /** * Check if value is defined (not null or undefined) */ export declare function isDefined(value: T | null | undefined): value is T; /** * Check if value is a function */ export declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown; /** * Check if value is an object (excludes null and arrays) */ export declare function isObject(value: unknown): value is Record; /** * Check if value is a plain object (not a class instance) */ export declare function isPlainObject(value: unknown): value is Record; /** * Check if value is an array */ export declare function isArray(value: unknown): value is unknown[]; /** * Check if value is an array of specific type */ export declare function isArrayOf(value: unknown, guard: TypeGuard): value is T[]; /** * Check if value is a Date */ export declare function isDate(value: unknown): value is Date; /** * Check if value is a RegExp */ export declare function isRegExp(value: unknown): value is RegExp; /** * Check if value is a Map */ export declare function isMap(value: unknown): value is Map; /** * Check if value is a Set */ export declare function isSet(value: unknown): value is Set; /** * Check if value is a Promise */ export declare function isPromise(value: unknown): value is Promise; /** * Check if value is an Error */ export declare function isError(value: unknown): value is Error; /** * Check if value is an integer */ export declare function isInteger(value: unknown): value is number; /** * Check if value is a positive number */ export declare function isPositive(value: unknown): value is number; /** * Check if value is a negative number */ export declare function isNegative(value: unknown): value is number; /** * Check if value is a non-negative number */ export declare function isNonNegative(value: unknown): value is number; /** * Check if value is a finite number */ export declare function isFinite(value: unknown): value is number; /** * Check if number is within range (inclusive) */ export declare function isInRange(value: unknown, min: number, max: number): value is number; /** * Check if value is a non-empty string */ export declare function isNonEmptyString(value: unknown): value is string; /** * Check if string matches pattern */ export declare function isStringMatching(value: unknown, pattern: RegExp): value is string; /** * Check if value is a valid email */ export declare function isEmail(value: unknown): value is string; /** * Check if value is a valid URL */ export declare function isURL(value: unknown): value is string; /** * Check if value is a valid UUID */ export declare function isUUID(value: unknown): value is string; /** * Check if value is a valid ISO date string */ export declare function isISODateString(value: unknown): value is string; /** * Check if value is a valid JSON string */ export declare function isJSONString(value: unknown): value is string; /** * Check if object has a specific key */ export declare function hasKey(value: unknown, key: K): value is Record; /** * Check if object has specific keys */ export declare function hasKeys(value: unknown, keys: K[]): value is Record; /** * Check if object has key with specific type */ export declare function hasKeyOfType(value: unknown, key: K, guard: TypeGuard): value is Record; /** * Create a shape guard for objects */ export declare function isShapeOf>>(shape: T): TypeGuard<{ [K in keyof T]: T[K] extends TypeGuard ? U : never; }>; /** * Check if value is one of the specified values */ export declare function isOneOf(value: unknown, values: T): value is T[number]; /** * Check if value is a specific literal */ export declare function isLiteral(value: unknown, literal: T): value is T; /** * Create a union type guard */ export declare function isUnion[]>(...guards: T): TypeGuard ? U : never>; /** * Create an intersection type guard */ export declare function isIntersection[]>(...guards: T): TypeGuard[] ? U : never>; /** * Assertion error class */ export declare class AssertionError extends Error { readonly received: unknown; readonly expected?: string; constructor(message: string, received: unknown, expected?: string); } /** * Create an assertion function from a type guard */ export declare function createAssertion(guard: TypeGuard, defaultMessage: string): TypeAssertion; /** * Assert that value is a string */ export declare function assertString(value: unknown, message?: string): asserts value is string; /** * Assert that value is a number */ export declare function assertNumber(value: unknown, message?: string): asserts value is number; /** * Assert that value is a boolean */ export declare function assertBoolean(value: unknown, message?: string): asserts value is boolean; /** * Assert that value is an object */ export declare function assertObject(value: unknown, message?: string): asserts value is Record; /** * Assert that value is an array */ export declare function assertArray(value: unknown, message?: string): asserts value is unknown[]; /** * Assert that value is defined (not null or undefined) */ export declare function assertDefined(value: T | null | undefined, message?: string): asserts value is T; /** * Assert that value matches a type guard */ export declare function assertType(value: unknown, guard: TypeGuard, message?: string): asserts value is T; /** * Assert condition is truthy */ export declare function assert(condition: unknown, message?: string): asserts condition; /** * Narrow value to type if guard passes, otherwise return undefined */ export declare function narrow(value: unknown, guard: TypeGuard): T | undefined; /** * Narrow value to type if guard passes, otherwise return default */ export declare function narrowOrDefault(value: unknown, guard: TypeGuard, defaultValue: T): T; /** * Narrow value with transform */ export declare function narrowAndTransform(value: unknown, guard: TypeGuard, transform: (value: T) => U): U | undefined; /** * Create a safe accessor for object properties */ export declare function safeGet(obj: unknown, key: string, guard: TypeGuard): T | undefined; /** * Create a deep safe accessor */ export declare function safeGetPath(obj: unknown, path: string[], guard: TypeGuard): T | undefined; /** * Check value and return result object */ export declare function check(value: unknown, guard: TypeGuard): CheckResult; /** * Check value with custom error message */ export declare function checkWithError(value: unknown, guard: TypeGuard, errorMessage: string): CheckResult; /** * Chain multiple checks */ export declare function checkAll]>>(checks: T): { success: boolean; values: { [K in keyof T]: T[K][1] extends TypeGuard ? U | undefined : never; }; errors: string[]; }; /** * Create a composable type guard */ export declare function createGuard(checker: (value: unknown) => boolean): TypeGuard; /** * Create a refinement guard */ export declare function refine(guard: TypeGuard, refinement: (value: T) => value is U): TypeGuard; /** * Create an optional guard */ export declare function optional(guard: TypeGuard): TypeGuard; /** * Create a nullable guard */ export declare function nullable(guard: TypeGuard): TypeGuard; /** * Create a record guard */ export declare function record(valueGuard: TypeGuard): TypeGuard>; /** * Create a tuple guard */ export declare function tuple[]>(...guards: T): TypeGuard<{ [K in keyof T]: T[K] extends TypeGuard ? U : never; }>; /** * Type guard namespace */ export declare const is: { readonly string: typeof isString; readonly number: typeof isNumber; readonly boolean: typeof isBoolean; readonly bigint: typeof isBigInt; readonly symbol: typeof isSymbol; readonly undefined: typeof isUndefined; readonly null: typeof isNull; readonly nullish: typeof isNullish; readonly defined: typeof isDefined; readonly function: typeof isFunction; readonly object: typeof isObject; readonly plainObject: typeof isPlainObject; readonly array: typeof isArray; readonly arrayOf: typeof isArrayOf; readonly date: typeof isDate; readonly regExp: typeof isRegExp; readonly map: typeof isMap; readonly set: typeof isSet; readonly promise: typeof isPromise; readonly error: typeof isError; readonly integer: typeof isInteger; readonly positive: typeof isPositive; readonly negative: typeof isNegative; readonly nonNegative: typeof isNonNegative; readonly finite: typeof isFinite; readonly inRange: typeof isInRange; readonly nonEmptyString: typeof isNonEmptyString; readonly stringMatching: typeof isStringMatching; readonly email: typeof isEmail; readonly url: typeof isURL; readonly uuid: typeof isUUID; readonly isoDateString: typeof isISODateString; readonly jsonString: typeof isJSONString; readonly hasKey: typeof hasKey; readonly hasKeys: typeof hasKeys; readonly hasKeyOfType: typeof hasKeyOfType; readonly shapeOf: typeof isShapeOf; readonly oneOf: typeof isOneOf; readonly literal: typeof isLiteral; readonly union: typeof isUnion; readonly intersection: typeof isIntersection; }; /** * Assertion namespace */ export declare const assertIs: { readonly string: typeof assertString; readonly number: typeof assertNumber; readonly boolean: typeof assertBoolean; readonly object: typeof assertObject; readonly array: typeof assertArray; readonly defined: typeof assertDefined; readonly type: typeof assertType; readonly that: typeof assert; }; export {};