tiny-types
Version:
A tiny library that brings Tiny Types to JavaScript and TypeScript
27 lines (26 loc) • 1.13 kB
TypeScript
import { Predicate } from './predicates';
/**
* @desc The `ensure` function verifies if the value meets the specified {Predicate}s.
*
* @example <caption>Basic usage</caption>
* import { ensure, isDefined } from 'tiny-types'
*
* const username = 'jan-molak'
* ensure('Username', username, isDefined());
*
* @example <caption>Ensuring validity of a domain object upon creation</caption>
* import { TinyType, ensure, isDefined, isInteger, isInRange } from 'tiny-types'
*
* class Age extends TinyType {
* constructor(public readonly value: number) {
* ensure('Age', value, isDefined(), isInteger(), isInRange(0, 125));
* }
* }
*
* @param {string} name - the name of the value to check.
* This name will be included in the error message should the check fail
* @param {T} value - the argument to check
* @param {...Array<Predicate<T>>} predicates - a list of predicates to check the value against
* @returns {T} - if the original value passes all the predicates, it's returned from the function
*/
export declare function ensure<T>(name: string, value: T, ...predicates: Array<Predicate<T>>): T;