/** * @module types/guards * @description Type guard functions */ import { z } from "zod/v4"; import type { BaseOptions, ClientConfig } from "./options.js"; /** * Check if a value is a Zod schema. * @param value The value to check * @returns True if the value is a Zod schema * * @example * ```typescript * import { z } from 'zod/v4'; * * const schema = z.string(); * console.log(isZodSchema(schema)); // true * console.log(isZodSchema('not a schema')); // false * console.log(isZodSchema({})); // false * ``` */ export declare function isZodSchema(value: unknown): value is z.ZodType; /** * Check if a value is a string. * @param value The value to check * @returns True if the value is a string * * @example * ```typescript * console.log(isString('hello')); // true * console.log(isString('')); // true * console.log(isString(123)); // false * console.log(isString(null)); // false * ``` */ export declare function isString(value: unknown): value is string; /** * Check if a value is a number. * @param value The value to check * @returns True if the value is a number (including NaN) * * @example * ```typescript * console.log(isNumber(123)); // true * console.log(isNumber(0)); // true * console.log(isNumber(NaN)); // true * console.log(isNumber('123')); // false * ``` */ export declare function isNumber(value: unknown): value is number; /** * Check if a value is a boolean. * @param value The value to check * @returns True if the value is a boolean * * @example * ```typescript * console.log(isBoolean(true)); // true * console.log(isBoolean(false)); // true * console.log(isBoolean(1)); // false * console.log(isBoolean('true')); // false * ``` */ export declare function isBoolean(value: unknown): value is boolean; /** * Check if a value is an object (and not null or array). * @param value The value to check * @returns True if the value is a plain object * * @example * ```typescript * console.log(isObject({})); // true * console.log(isObject({ key: 'value' })); // true * console.log(isObject([])); // false - arrays are not plain objects * console.log(isObject(null)); // false * ``` */ export declare function isObject(value: unknown): value is Record; /** * Check if a value is null or undefined. * @param value The value to check * @returns True if the value is null or undefined * * @example * ```typescript * console.log(isNullOrUndefined(null)); // true * console.log(isNullOrUndefined(undefined)); // true * console.log(isNullOrUndefined(0)); // false * console.log(isNullOrUndefined('')); // false * ``` */ export declare function isNullOrUndefined(value: unknown): value is null | undefined; /** * Check if a value is defined (not null or undefined). * @param value The value to check * @returns True if the value is defined * * @example * ```typescript * const value: string | undefined = process.env.VAR; * if (isDefined(value)) { * // TypeScript knows value is string here * console.log(value.toUpperCase()); * } * ``` */ export declare function isDefined(value: T | null | undefined): value is T; /** * Check if a value has a specific property. * @param value The value to check * @param property The property name to check for * @returns True if the value has the property * * @example * ```typescript * const obj = { name: 'test', age: 30 }; * if (hasProperty(obj, 'name')) { * // TypeScript knows obj has 'name' property * console.log(obj.name); * } * ``` */ export declare function hasProperty(value: unknown, property: K): value is Record; /** * Check if a value has BaseOptions shape. * @param value The value to check * @returns True if the value matches BaseOptions interface * * @example * ```typescript * const valid = { options: { default: 'test', client: { expose: true } } }; * console.log(hasBaseOptions(valid)); // true * * const invalid = { options: { unknownKey: 'test' } }; * console.log(hasBaseOptions(invalid)); // false * ``` */ export declare function hasBaseOptions(value: unknown): value is { options?: BaseOptions; }; /** * Check if a value is a valid ClientConfig. * @param value The value to check * @returns True if the value is a valid ClientConfig * * @example * ```typescript * const valid = { expose: true, transform: (v: string) => v.toUpperCase() }; * console.log(isClientConfig(valid)); // true * * const invalid = { expose: 'yes' }; // expose must be boolean * console.log(isClientConfig(invalid)); // false * ``` */ export declare function isClientConfig(value: unknown): value is ClientConfig; /** * Check if a string starts with any of the given prefixes. * @param str The string to check * @param prefixes The prefixes to check against * @returns True if the string starts with any prefix * * @example * ```typescript * console.log(startsWithAny('NEXT_PUBLIC_API', ['NEXT_PUBLIC_', 'VITE_'])); // true * console.log(startsWithAny('SECRET_KEY', ['NEXT_PUBLIC_', 'VITE_'])); // false * console.log(startsWithAny('VITE_APP_TITLE', ['NEXT_PUBLIC_', 'VITE_'])); // true * ``` */ export declare function startsWithAny(str: string, prefixes: string[]): boolean; /** * Safely get a property from an object. * @param obj The object to get the property from * @param key The property key * @returns The property value or undefined * * @example * ```typescript * const obj = { name: 'test' }; * console.log(safeGet(obj, 'name')); // 'test' * console.log(safeGet(obj, 'unknown' as any)); // undefined * * // Handles property access errors gracefully * const proxy = new Proxy({}, { get: () => { throw new Error(); } }); * console.log(safeGet(proxy, 'any')); // undefined * ``` */ export declare function safeGet(obj: T, key: K): T[K] | undefined; /** * Check if a value is a valid environment variable value (string or undefined). * @param value The value to check * @returns True if the value is a valid env var value * * @example * ```typescript * console.log(isEnvValue('production')); // true * console.log(isEnvValue(undefined)); // true * console.log(isEnvValue(123)); // false - numbers not valid * console.log(isEnvValue(null)); // false - null not valid * ``` */ export declare function isEnvValue(value: unknown): value is string | undefined; /** * Ensure a value is a string, converting if necessary. * @param value The value to ensure as string * @returns The value as a string * @throws {Error} Error if the value cannot be converted to string * * @example * ```typescript * console.log(ensureString('hello')); // 'hello' * console.log(ensureString(123)); // '123' * console.log(ensureString(true)); // 'true' * console.log(ensureString({ key: 'value' })); // '{"key":"value"}' * * try { * ensureString(null); // throws Error * } catch (e) { * console.error(e.message); // 'Cannot convert null or undefined to string' * } * ``` */ export declare function ensureString(value: unknown): string; /** * Ensure a value is a number, converting if necessary. * @param value The value to ensure as number * @returns The value as a number * @throws {Error} Error if the value cannot be converted to number * * @example * ```typescript * console.log(ensureNumber(123)); // 123 * console.log(ensureNumber('456')); // 456 * console.log(ensureNumber('3.14')); // 3.14 * * try { * ensureNumber('not a number'); // throws Error * } catch (e) { * console.error(e.message); // 'Cannot convert "not a number" to number' * } * ``` */ export declare function ensureNumber(value: unknown): number; /** * Ensure a value is a boolean, converting if necessary. * @param value The value to ensure as boolean * @returns The value as a boolean * * @example * ```typescript * console.log(ensureBoolean(true)); // true * console.log(ensureBoolean('true')); // true * console.log(ensureBoolean('yes')); // true * console.log(ensureBoolean('1')); // true * console.log(ensureBoolean('false')); // false * console.log(ensureBoolean('no')); // false * console.log(ensureBoolean(0)); // false * console.log(ensureBoolean(1)); // true * ``` */ export declare function ensureBoolean(value: unknown): boolean; /** * Type guard to check if an error is a Zod error. * @param error The error to check * @returns True if the error is a ZodError * * @example * ```typescript * import { z } from 'zod/v4'; * * try { * z.string().parse(123); * } catch (error) { * if (isZodError(error)) { * console.log('Zod validation failed'); * error.issues.forEach(issue => { * console.log(issue.message); * }); * } * } * ``` */ export declare function isZodError(error: unknown): error is z.ZodError; /** * Type guard to check if an error is a standard Error. * @param error The error to check * @returns True if the error is an Error * * @example * ```typescript * const err1 = new Error('test'); * console.log(isError(err1)); // true * * const err2 = { name: 'Error', message: 'test' }; * console.log(isError(err2)); // true - error-like object * * console.log(isError('error string')); // false * console.log(isError(null)); // false * ``` */ export declare function isError(error: unknown): error is Error; /** * Get the message from an error safely. * @param error The error to get the message from * @returns The error message or a default message * * @example * ```typescript * console.log(getErrorMessage(new Error('test'))); // 'test' * console.log(getErrorMessage('string error')); // 'string error' * console.log(getErrorMessage(null)); // 'null' * console.log(getErrorMessage(undefined)); // 'undefined' * console.log(getErrorMessage(123)); // '123' * console.log(getErrorMessage({})); // 'Unknown error' * ``` */ export declare function getErrorMessage(error: unknown): string; /** * Create a type guard function for a specific type. * This is a utility function that can be used with generic types. * @param check The check function * @returns A type guard function * * @example * ```typescript * interface User { name: string; age: number } * * const isUser = createTypeGuard((value) => { * return isObject(value) && * 'name' in value && isString(value.name) && * 'age' in value && isNumber(value.age); * }); * * const data: unknown = { name: 'John', age: 30 }; * if (isUser(data)) { * // TypeScript knows data is User here * console.log(data.name); * } * ``` */ export declare function createTypeGuard(check: (value: unknown) => boolean): (value: unknown) => value is T; /** * Compose multiple type guards with AND logic. * @param guards The type guards to compose * @returns A composed type guard * * @example * ```typescript * interface Config { port: number; host: string } * * const hasPort = (v: unknown): v is { port: number } => * isObject(v) && 'port' in v && isNumber(v.port); * * const hasHost = (v: unknown): v is { host: string } => * isObject(v) && 'host' in v && isString(v.host); * * const isConfig = composeGuards(hasPort, hasHost); * * const valid = { port: 3000, host: 'localhost' }; * console.log(isConfig(valid)); // true * ``` */ export declare function composeGuards(...guards: ((value: unknown) => value is T)[]): (value: unknown) => value is T; /** * Create a type guard for an object with specific properties. * @param props The properties to check for * @returns A type guard function * * @example * ```typescript * const hasUserProps = hasProperties('name', 'email', 'age'); * * const valid = { name: 'John', email: 'john@example.com', age: 30 }; * console.log(hasUserProps(valid)); // true * * const invalid = { name: 'John' }; // missing email and age * console.log(hasUserProps(invalid)); // false * ``` */ export declare function hasProperties(...props: K[]): (value: unknown) => value is Record;