UNPKG

@dozerg/condition

Version:

Preconditions utility for TypeScript and JavaScript.

218 lines (217 loc) 7.13 kB
/** * Check if a condition is true, or throw an error with custom message. * @example * ```ts * const a: unknown = 'abc'; * // a.length; // expect a compiler error * * assertTrue(typeof a === 'string'); * a.length; // OK, a is string * ``` * * @param condition - The condition to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare function assertTrue(condition: boolean, message?: string, props?: object): asserts condition; /** * Check if a value is neither `null` nor `undefined`. * @example * ```ts * const a = [1, undefined, 2]; // a is (number | undefined)[] * const b = a.filter(isNonNull); // b is number[] * ``` * * @param value - The value to be checked * @returns false if the value is `null` or `undefined`, or true otherwise */ export declare function isNonNull<T>(value: T): value is NonNullable<T>; /** * Check if a value is neither `null` nor `undefined`, or throw an error with custom message. * @example * ```ts * function f(v?: string) { * //v.length; // expect a compiler error * assertIsNonNull(v); * v.length; // OK, v is string * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare function assertNonNull<T>(value: T, message?: string, props?: object): asserts value is NonNullable<T>; /** * Check if a value is number. * @example * ```ts * const a = ['1', 2, '3', 4]; // a is (string | number)[] * const b = a.filter(isNumber); // b is [2, 4], i.e. number[] * ``` * * @param value - The value to be checked * @returns true if the value is number, or false otherwise */ export declare const isNumber: (value: any) => value is number; /** * Check if a value is string. * @example * ```ts * const a = ['1', 2, '3', 4]; // a is (string | number)[] * const b = a.filter(isString); // b is ['1', '3'], i.e. string[] * ``` * * @param value - The value to be checked * @returns true if the value is string, or false otherwise */ export declare const isString: (value: any) => value is string; /** * Check if a value is boolean. * @example * ```ts * const a = ['1', true, '3', false]; // a is (string | boolean)[] * const b = a.filter(isBoolean); // b is [true, false], i.e. boolean[] * ``` * * @param value - The value to be checked * @returns true if the value is boolean, or false otherwise */ export declare const isBoolean: (value: any) => value is boolean; /** * Check if a value is bigint. * @example * ```ts * const a = [1, BigInt(2), 3, BigInt(4)]; // a is (number | bigint)[] * const b = a.filter(isBigint); // b is [BigInt(2), BigInt(3)], i.e. bigint[] * ``` * * @param value - The value to be checked * @returns true if the value is bigint, or false otherwise */ export declare const isBigint: (value: any) => value is bigint; /** * Check if a value is an object. * @param v - The value to be checked * @returns true if the value is an object, or false otherwise */ export declare function isObject(v: any): boolean; /** * Returns a function to check if a value is instance of a class. * @example * ```ts * class A {}; * class B {}; * const a = [new A(), 1, new B()]; // a is (number | A | B)[] * const b = a.filter(isClass(A)); // b is [new A()], i.e. A[] * ``` * * @param Class - The constructor of a class * @returns A function */ export declare function isClass<T extends { new (...args: any[]): unknown; }>(Class: T): (value: any) => value is InstanceType<T>; declare const _assertIsNumber: (value: any, message?: string, props?: object) => asserts value is number; declare const _assertIsString: (value: any, message?: string, props?: object) => asserts value is string; declare const _assertIsBoolean: (value: any, message?: string, props?: object) => asserts value is boolean; declare const _assertIsBigint: (value: any, message?: string, props?: object) => asserts value is bigint; /** * Check if a value is number, or throw an error with custom message. * @example * ```ts * function f(v: boolean | number) { * //v + 1; // expect a compiler error * assertIsNumber(v); * v + 1; // OK, v is number * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare const assertIsNumber: typeof _assertIsNumber; /** * Check if a value is string, or throw an error with custom message. * @example * ```ts * function f(v: boolean | string) { * //v.length; // expect a compiler error * assertIsString(v); * v.length; // OK, v is string * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare const assertIsString: typeof _assertIsString; /** * Check if a value is boolean, or throw an error with custom message. * @example * ```ts * function f(v: A) {} * * function g(v: boolean | string) { * // f(v); // expect a compiler error * assertIsBoolean(v); * f(v); // OK, v is boolean * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare const assertIsBoolean: typeof _assertIsBoolean; /** * Check if a value is bigint, or throw an error with custom message. * @example * ```ts * function f(v: bigint) {} * * function g(v: bigint | string) { * // f(v); // expect a compiler error * assertIsBigint(v); * f(v); // OK, v is bigint * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare const assertIsBigint: typeof _assertIsBigint; /** * Check if a value is an object, or throw an error with custom message. * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare function assertIsObject(value: any, message?: string, props?: object): void; /** * Check if a value is instance of a class, or throw an error with custom message. * @example * ```ts * class A { * a() {} * } * * function f(v: A | string) { * // v.a(); // expect a compiler error * assertIsClass(A, v); * v.a(); // OK, v is class A * } * ``` * * @param value - The value to be checked * @param message - Error message, or absent for default message * @param props - Extra properties to be added to error message */ export declare function assertIsClass<T extends { new (...args: any[]): unknown; }>(Class: T, value: any, message?: string, props?: object): asserts value is InstanceType<T>; export {};