@snapdm/preconditions
Version:
General purpose preconditions for typesafe Typescript libraries
47 lines (42 loc) • 3.07 kB
TypeScript
declare type Type<T> = new (...args: unknown[]) => T;
/**
* A Predicate is a function that takes a value, with some optional other values,
* and determines whether the given value satifies a type predicate.
*/
declare type Predicate<T> = (value: unknown, ...others: unknown[]) => value is T;
/**
* This condition checks the whether the given value is defined or not.
* @param value Value to check is defined.
*/
declare function isDefined<T>(value: T | null | undefined): value is T;
declare function isTruthy<T>(value: T | "" | null | undefined | 0 | 0n | false): value is T;
declare function isFalsy<T>(value: T | "" | null | undefined | 0 | 0n | false): value is "" | null | undefined | 0 | 0n | false;
declare function isUndefined(value: unknown): value is undefined;
declare function isNull(value: unknown): value is null;
declare function isBoolean(value: unknown): value is boolean;
declare function isString(value: unknown): value is string;
declare function isNumber(value: unknown): value is number;
declare function isBigInt(value: unknown): value is bigint;
declare function isFunction(value: unknown): value is Function;
declare function isObject(value: unknown): value is object;
declare function isType<T>(value: unknown, type: Type<T>): value is T;
/**
* PreconditionFailedError is an error to throw when a precondition fails.
*/
declare class PreconditionFailedError extends Error {
constructor(message?: string);
}
declare function assert<T extends boolean>(condition: T, message?: string): asserts condition;
declare function assertPredicate<T>(condition: Predicate<T>, value: unknown, message?: string): asserts value is T;
declare function assertIsDefined<T>(value: T | null | undefined, message?: string): asserts value is T;
declare function assertIsUndefined(value: unknown, message?: string): asserts value is undefined;
declare function assertIsNull(value: unknown, message?: string): asserts value is null;
declare function assertIsBoolean(value: unknown, message?: string): asserts value is boolean;
declare function assertIsString(value: unknown, message?: string): asserts value is string;
declare function assertIsNumber(value: unknown, message?: string): asserts value is number;
declare function assertIsBigInt(value: unknown, message?: string): asserts value is bigint;
declare function assertIsFunction(value: unknown, message?: string): asserts value is Function;
declare function assertIsObject(value: unknown, message?: string): asserts value is object;
declare function assertIsType<T>(value: unknown, type: Type<T>, message?: string): asserts value is T;
declare function checkIsDefined<T>(value: T | null | undefined, message?: string): T;
export { PreconditionFailedError, Predicate, assert, assertIsBigInt, assertIsBoolean, assertIsDefined, assertIsFunction, assertIsNull, assertIsNumber, assertIsObject, assertIsString, assertIsType, assertIsUndefined, assertPredicate, checkIsDefined, isBigInt, isBoolean, isDefined, isFalsy, isFunction, isNull, isNumber, isObject, isString, isTruthy, isType, isUndefined };