UNPKG

pure-parse

Version:

Strongly typed validation library that decouples type aliases from validation logic

37 lines (36 loc) 1.47 kB
import { Guard, OptionalGuard } from './Guard'; /** * Represent an optional property. Note that in TypeScript, optional properties may be assigned `undefined` or omitted entirely from the object. * @example * Wrap properties in `optional` to make them optional: * ```ts * type User = { * id: number * email?: string * } * const isUser = objectGuard<User>({ * id: isNumber, * email: optionalGuard(isString), * }) * isUser({ id: 123 }) // -> true * isUser({ id: 123, email: undefined }) // -> true * isUser({ id: 123, email: 'abc@test.com' }) // -> true * ``` * @param guard */ export declare const optionalGuard: <T>(guard: Guard<T>) => OptionalGuard<T>; /** * Create an optional property that also can be `null`. Convenient when creating optional nullable properties in objects. Alias for `optional(oneOfGuard(isNull, guard))`. * @param guard */ export declare const optionalNullableGuard: <T>(guard: Guard<T>) => OptionalGuard<T | null>; /** * Create a union with `null`. Convenient when creating nullable properties in objects. Alias for `unionGuard(isNull, guard)`. * @param guard */ export declare const nullableGuard: <T>(guard: Guard<T>) => (data: unknown) => data is T | null; /** * Create a union with `undefined`, which is different from optional properties. Alias for `unionGuard(isUndefined, guard)`. * @param guard */ export declare const undefineableGuard: <T>(guard: Guard<T>) => (data: unknown) => data is T | undefined;