ts-predicate
Version:
TypeScript predicates and assertions library
191 lines (190 loc) • 6.56 kB
TypeScript
import type { ArrayConstraints, ObjectWithNullableProperty, ObjectWithProperty, PopulatedArray, StringConstraints } from "./Types.js";
/**
* TypeAssertion
*
* @class
*/
declare class TypeAssertion {
/**
* IsDefined
*
* @description Assertion that check if a value is defined.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is undefined, null or NaN
* @return {void} nothing
*/
static IsDefined<Type>(value: Type): asserts value is NonNullable<Type>;
/**
* IsBoolean
*
* @description Assertion that check if a value is a boolean.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a boolean
* @return {void} nothing
*/
static IsBoolean(value: unknown): asserts value is boolean;
/**
* IsNumber
*
* @description Assertion that check if a value is a number.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a number
* @return {void} nothing
*/
static IsNumber(value: unknown): asserts value is number;
/**
* IsInteger
*
* @description Assertion that check if a value is an integer.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not an integer
* @return {void} nothing
*/
static IsInteger(value: unknown): asserts value is number;
/**
* IsFiniteNumber
*
* @description Assertion that check if a value is a finite number.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a finite number
* @return {void} nothing
*/
static IsFiniteNumber(value: unknown): asserts value is number;
/**
* IsString
*
* @description Assertion that check if a value is a string.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {number} [constraints.maxLength] maximum length
* @throws {Error} if the value is not a string
* @return {void} nothing
*/
static IsString(value: unknown, constraints?: StringConstraints): asserts value is string;
/**
* IsFilledString
*
* @description Assertion that check if a value is non empty string.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a string
* @return {void} nothing
*/
static IsFilledString(value: unknown): asserts value is string;
/**
* IsArray
*
* @description Assertion that check if a value is an array.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {void} [constraints.itemGuard] another predicate
* @throws {Error} if the value is not an array
* @throws {Error} if the array does not have enough items {@link constraints.minLength}
* @throws {Error} if the array has some item which does not comply the {@link constraints.itemGuard} predicate
* @return {void} nothing
*/
static IsArray<Type>(value: unknown, constraints?: ArrayConstraints<Type>): asserts value is Array<Type>;
/**
* IsPopulatedArray
*
* @description Assertion that check if a value is an array with at least one item.
* @public
* @static
* @param {unknown} value the value
* @param {object} [constraints] some additional check
* @param {number} [constraints.minLength] minimal length
* @param {void} [constraints.itemGuard] another predicate
* @throws {Error} if the value is not an array
* @throws {Error} if the array does not have enough items {@link constraints.minLength}
* @throws {Error} if the array has some item which does not comply the {@link constraints.itemGuard} predicate
* @return {void} nothing
*/
static IsPopulatedArray<Type>(value: unknown, constraints?: ArrayConstraints<Type>): asserts value is PopulatedArray<Type>;
/**
* IsRecord
*
* @description Assertion that check if a value is a record.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a record
* @return {void} nothing
*/
static IsRecord<KeyType extends number | string | symbol = string>(value: unknown): asserts value is Record<KeyType, unknown>;
/**
* IsObject
*
* @description Assertion that check if a value is an object.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not an object
* @return {void} nothing
*/
static IsObject(value: unknown): asserts value is object;
/**
* IsFunction
*
* @description Assertion that check if a value is a function.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not a function
* @return {void} nothing
*/
static IsFunction(value: unknown): asserts value is Function;
/**
* IsCallable
*
* @description Assertion that check if a value is callable.
* @public
* @static
* @param {unknown} value the value
* @throws {Error} if the value is not callable
* @return {void} nothing
*/
static IsCallable(value: unknown): asserts value is Function;
/**
* HasNullableProperty
*
* @description Predicate that check if an object as a nullable property.
* @public
* @static
* @param {object} value the object
* @param {string} property the property
* @throws {Error} if the value does not have a property named {@link property}
* @return {void} nothing
*/
static HasNullableProperty<O extends object, K extends string>(value: O, property: K): asserts value is ObjectWithNullableProperty<O, K>;
/**
* HasProperty
*
* @description Predicate that check if an object as a property.
* @public
* @static
* @param {object} value the object
* @param {string} property the property
* @throws {Error} if the value does not have a property named {@link property}
* @throws {Error} if the {@link property} is undefined, null or NaN
* @return {void} nothing
*/
static HasProperty<O extends object, K extends string>(value: O, property: K): asserts value is ObjectWithProperty<O, K>;
}
export { TypeAssertion };