narrows
Version:
Super lean and simple object validation with TypeScript support.
47 lines • 3.37 kB
TypeScript
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
declare type InstanceOf<T> = T extends {
new (...args: any[]): infer U;
} ? U : never;
/** Validator for type T. */
export declare type Validator<T = unknown> = (x: unknown) => x is T;
/** Type for which a validator matches. */
export declare type TypeOf<T> = T extends Validator<infer U> ? U : never;
/** Returns true if and only if x is a boolean. */
export declare const boolean: (x: unknown) => x is boolean;
/** Returns true if and only if x is a string. */
export declare const string: (x: unknown) => x is string;
/** Returns true if and only if x is a number. */
export declare const number: (x: unknown) => x is number;
/** Returns true if and only if x is undefined. */
export declare const empty: (x: unknown) => x is undefined;
/** Returns true if and only if x is null. */
export declare const nil: (x: unknown) => x is null;
/** Returns true if and only if x is strictly equal to y. */
export declare const literal: <T extends string | number | boolean | symbol>(y: T) => (x: unknown) => x is T;
/** Returns true if and only if x is an object where each value matches the given validator. */
export declare const object: <T extends Validator<unknown>>(validator: T) => (x: unknown) => x is {
[key: string]: TypeOf<T>;
};
/** Returns true if and only if x is an array where each element matches the given validator. */
export declare const array: <T extends Validator<unknown>>(validator: T) => (x: unknown) => x is TypeOf<T>[];
/** Returns true if and only if x is an instance of the given type. */
export declare const instance: <T extends Function>(base: T) => (x: unknown) => x is InstanceOf<T>;
/** Returns true if and only if x is an object where each value matches the validator at the corresponding key in the schema. */
export declare const record: <T extends {
[key: string]: Validator<unknown>;
}>(schema: T) => (x: unknown) => x is { [K in keyof T]: TypeOf<T[K]>; };
/** Returns true if and only if x is an array where each element matches the validator at the corresponding index in the schema. */
export declare const tuple: <T extends Validator<unknown>[]>(...schema: T) => (x: unknown) => x is { [K in keyof T]: TypeOf<T[K]>; };
/** Returns true if and only if x matches any of the given validators. */
export declare const any: <T extends Validator<unknown>[]>(...validators: T) => (x: unknown) => x is TypeOf<T[keyof T]>;
/** Returns true if and only if x matches all of the given validators. */
export declare const all: <T extends Validator<unknown>[]>(...validators: T) => (x: unknown) => x is UnionToIntersection<TypeOf<T[keyof T]>>;
/** Returns true if and only if x matches the given validator or is undefined. */
export declare const optional: <T>(validator: Validator<T>) => (x: unknown) => x is T | undefined;
/** Returns true if and only if x matches the given validator or is null. */
export declare const nullable: <T>(validator: Validator<T>) => (x: unknown) => x is T | null;
declare type Path = Array<string | number | symbol>;
/** Takes a validator and an object to be validated. Returns null if the object is valid, or the path to the invalid property. */
export declare function report(validate: (x: any) => boolean, object: any): Path | null;
export {};
//# sourceMappingURL=index.d.ts.map