assertate
Version:
TypeScript assertion helpers
199 lines (198 loc) • 8.16 kB
TypeScript
/**
* Returns a refined type of an object. Defaults to `typeof` unless the value is
* null, in which case 'null' is returned
*
* @param value value to get the type of
*/
export declare function getType(value: unknown): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null";
export declare type AssertionMessageType = (value: unknown, type: string, variableName?: string, additionalMessage?: string) => string;
/**
* Sets the global AssertionMessage with the `message` provided
*
* @param message assertion message generator to replace the current generator with
*/
export declare function setAssertionMessage(message: AssertionMessageType): void;
/**
* Gets the currently set global AssertionMessage
*/
export declare function getAssertionMessage(): AssertionMessageType;
/**
* Returns the boolean and hoisted type-check value that `value` is a number
*
* @param value value to type-check as a number
*/
export declare function isNumber(value: unknown): value is number;
/**
* Returns the boolean and hoisted type-check value that `value` is a bigint
*
* @param value value to type-check as a bigint
*/
export declare function isBigInt(value: unknown): value is bigint;
/**
* Returns the boolean and hoisted type-check value that `value` is a boolean
*
* @param value value to type-check as a boolean
*/
export declare function isBoolean(value: unknown): value is boolean;
/**
* Returns the boolean and hoisted type-check value that `value` is a string
*
* @param value value to type-check as a string
*/
export declare function isString(value: unknown): value is string;
/**
* Returns the boolean and hoisted type-check value that `value` is an array
*
* @param value value to type-check as an array
*/
export declare function isArray(value: unknown): value is unknown[];
/**
* Returns the boolean and hoisted type-check value that `value` is an object.
* null is not considered an 'object'.
*
* @param value value to type-check as an object
*/
export declare function isObject<T extends {
[key: string]: unknown;
}>(value: unknown): value is T;
/**
* Returns the boolean and hoisted type-check value that `value` is not undefined (NonNullable)
*
* @param value value to type-check as NonNullable
*/
export declare function isDefined(value: unknown): value is NonNullable<any>;
/**
* Returns the boolean and hoisted type-check value that `value` is undefined
*
* @param value value to type-check as undefined
*/
export declare function isUndefined(value: unknown): value is undefined;
/**
* Returns the boolean and hoisted type-check value that `value` is undefined
*
* @param value value to type-check as undefined
*/
export declare function isNotUndefined(value: unknown): value is undefined;
/**
* Returns the boolean and hoisted type-check value that `value` is null
*
* @param value value to type-check as null
*/
export declare function isNull(value: unknown): value is null;
/**
* Returns the boolean and hoisted type-check value that `value` is a symbol
*
* @param value value to type-check as a symbol
*/
export declare function isSymbol(value: unknown): value is symbol;
/**
* Type-checks the provided `value` to be a string, throws an Error if it is not
*
* @param value the value to type-check as a string
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsString(value: unknown, variableName?: string, additionalMessage?: string): asserts value is string;
/**
* Type-checks the provided `value` to be a boolean, throws an Error if it is
* not
*
* @param value the value to type-check as a boolean
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsBoolean(value: unknown, variableName?: string, additionalMessage?: string): asserts value is boolean;
/**
* Type-checks the provided 'value' to be a number, throws an Error if it is not
*
* @param value the value to type-check as a number
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsNumber(value: unknown, variableName?: string, additionalMessage?: string): asserts value is number;
/**
* Type-checks the provided 'value' to be a bigint, throws an Error if it is not
*
* @param value the value to type-check as a bigint
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsBigInt(value: unknown, variableName?: string, additionalMessage?: string): asserts value is bigint;
/**
* Type-checks the provided `value` to be an array, throws an Error if it is not
*
* @param value the value to type-check as an array
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsArray<T>(value: unknown, variableName?: string, additionalMessage?: string): asserts value is T[];
/**
* Type-checks the provided `value` to be null, throws an Error if it is not
*
* @param value the value to type-check as null
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsNull(value: unknown, variableName?: string, additionalMessage?: string): asserts value is null;
/**
* Type-checks the provided `value` to be an object, throws an Error if it is
* not
*
* @param value the value to type-check as an object
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsObject(value: unknown, variableName?: string, additionalMessage?: string): asserts value is object;
/**
* Type-checks the provided `value` to be a symbol, throws an Error if it is not
*
* @param value the value to type-check as a symbol
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsSymbol(value: unknown, variableName?: string, additionalMessage?: string): asserts value is symbol;
/**
* Type-checks the provided `value` to be undefined, throws an Error if it is
* not
*
* @param value the value to type-check as undefined
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsUndefined(value: unknown, variableName?: string, additionalMessage?: string): asserts value is undefined;
/**
* Type-checks the provided `value` to be not undefined, throws an Error if it is
* not
*
* @param value the value to type-check as undefined
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsNotUndefined(value: unknown, variableName?: string, additionalMessage?: string): asserts value is undefined;
/**
* Type-checks the provided `value` to be defined. throws and Error if it is not
*
* @param value the value to type-check as as defined
* @param variableName teh name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
export declare function assertIsDefined(value: unknown, variableName?: string, additionalMessage?: string): asserts value is NonNullable<any>;
/**
* General assertion helper. Asserts the provided `condition` is met, if it is
* not, throws an Error with the provided `message`
*
* @param condition the condition to be asserted
* @throws {Error}
*/
export declare function assert(condition: unknown, message?: string): asserts condition;