UNPKG

type-plus

Version:
110 lines 3.65 kB
import { tersify } from 'tersify'; import { isConstructor } from '../class/index.js'; import {} from '../function/any_function.js'; export function assertType(subject, validator) { if (validator) { if ((isConstructor(validator) && !(subject instanceof validator)) || !validator(subject)) throw new TypeError(`subject fails to satisfy ${tersify(validator)}`); } return; } assertType.isUndefined = function (subject) { if (typeof subject !== 'undefined') throw TypeError(`subject is not undefined`); }; assertType.noUndefined = function (subject) { if (typeof subject === 'undefined') throw TypeError(`subject is undefined`); }; assertType.isNull = function (subject) { if (subject !== null) throw TypeError(`subject is not null`); }; assertType.noNull = function (subject) { if (subject === null) throw TypeError(`subject is null`); }; assertType.isNumber = function (subject) { if (typeof subject !== 'number') throw TypeError(`subject is not number`); }; assertType.noNumber = function (subject) { if (typeof subject === 'number') throw TypeError(`subject is number`); }; assertType.isBoolean = function (subject) { if (typeof subject !== 'boolean') throw TypeError(`subject is not boolean`); }; assertType.noBoolean = function (subject) { if (typeof subject === 'boolean') throw TypeError(`subject is boolean`); }; assertType.isTrue = function (subject) { if (subject !== true) throw TypeError(`subject is not true`); }; assertType.noTrue = function (subject) { // @ts-ignore if (subject === true) throw TypeError(`subject is true`); }; assertType.isFalse = function (subject) { if (subject !== false) throw TypeError(`subject is not false`); }; assertType.noFalse = function (subject) { // @ts-ignore if (subject === false) throw TypeError(`subject is false`); }; assertType.isString = function (subject) { if (typeof subject !== 'string') throw TypeError(`subject is not string`); }; assertType.noString = function (subject) { if (typeof subject === 'string') throw TypeError(`subject is string`); }; assertType.isFunction = function (subject) { if (typeof subject !== 'function') throw TypeError(`subject is not function`); }; assertType.noFunction = function (subject) { if (typeof subject === 'function') throw TypeError(`subject is function`); }; /** * 💀 deprecated. It does not work in all cases. * * It passes for function that can be called with `new`. * If the subject is an arrow function, it can still return true after compilation. */ assertType.isConstructor = function (subject) { if (!isConstructor(subject)) throw TypeError(`subject is not a constructor`); }; assertType.isError = function (subject) { if (!(subject instanceof Error)) throw TypeError(`subject is not an Error`); }; assertType.noError = function (subject) { if (subject instanceof Error) throw TypeError(`subject is an Error`); }; assertType.isNever = function (_subject) { }; /** * creates a custom assertion function with standard TypeError. * Currently this requires explicity type annotation, * thus making it hard to use: * https://github.com/microsoft/TypeScript/issues/41047 */ assertType.custom = function (validator) { return function (subject) { if (!validator(subject)) throw new TypeError(`subject fails to satisfy ${tersify(validator)}`); return; }; }; assertType.as = function (_subject) { }; //# sourceMappingURL=assert_type.js.map