@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
113 lines (112 loc) • 3.75 kB
text/typescript
declare enum TypeCategory {
UNDEFINED = 0,
NULL = 1,
BOOLEAN = 2,
NUMBER = 3,
BIGINT = 4,
STRING = 5,
SYMBOL = 6,
ARRAY = 7,
FUNCTION = 8,
CLASS = 9
}
/**
* Describes the type of a value.
*/
declare class Type {
static readonly UNDEFINED: Type;
static readonly NULL: Type;
static readonly BOOLEAN: Type;
static readonly NUMBER: Type;
static readonly BIGINT: Type;
static readonly STRING: Type;
static readonly SYMBOL: Type;
static readonly ARRAY: Type;
/**
* An anonymous or arrow function.
*/
static readonly ANONYMOUS_FUNCTION: Type;
readonly category: TypeCategory;
readonly name: string | null;
readonly typeGuard?: (value: unknown) => boolean;
/**
* Returns the type of a value.
*
* @param value - a value
* @returns the value's type
* @see <a href="http://stackoverflow.com/a/332429/14731">http://stackoverflow.com/a/332429/14731</a>
* @see Type.isPrimitive
*/
static of(value: unknown): Type;
/**
* Returns the type of a named class.
*
* @param name - the name of the class, or `null` to represent any class.
* @param typeGuard - (optional) for certain types, such as Typescript interfaces, runtime validation is
* not possible. In such a case, use a type guard to check if the value satisfies the type condition.
* @returns the type
*/
static namedClass(name: string | null, typeGuard?: (value: unknown) => boolean): Type;
/**
* Returns the type of a named function.
*
* @param name - (optional) the name of the function. `name` represents any named function.
* @returns the type
*/
static namedFunction(name: string | null): Type;
/**
* Returns the type of an `undefined`, `null`, `boolean`, `number`, `bigint`, `string` or `symbol`
* value.
*
* @param value - a value
* @returns `null` if the value is not a primitive value
*/
static getPrimitive(value: unknown): Type | null;
/**
* Creates a new Type.
*
* @param category - the category of the type
* @param name - (optional) the name of the function or class. `null` represents any instance of the type.
* @param typeGuard - (optional) for certain types, such as Typescript interfaces, runtime validation is
* not possible. In such a case, use a type guard to check if the value satisfies the type condition.
* @throws RangeError if neither `type` nor `name` are set.
* If `type` does not have a name (e.g. "number" or "array") but `name` is set.
*/
private constructor();
/**
* @returns `true` if the type is an `undefined`, `null`, `boolean`, `number`, `bigint`, `string` or
* `symbol` value
*/
isPrimitive(): boolean;
/**
* Indicates if this type is equal to another type.
*
* @param other - another type
* @returns true if this type matches `other`
*/
equals(other: Type): boolean;
/**
* Returns the type of this type.
*
* @returns the type of this type
*/
getTypeOf(): Type;
/**
* Indicates whether this type is a subtype of another type. Note that types are considered subtypes of
* themselves.
*
* @param parent - the parent type
* @returns
* <ul>
* <li>`true` if `child` extends `parent`</li>
* <li>`false` if `parent` or `child` are `undefined`, `null` or an object</li>
* <li>`false` if `child` does not extend `parent`</li>
* </ul>
*/
isSubtypeOf(parent: Type): boolean;
/**
* @returns the string representation of this object
*/
toString(): string;
}
export { Type, TypeCategory };