type-plus
Version:
Provides additional types for TypeScript.
113 lines • 3.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertType = void 0;
const tersify_1 = require("tersify");
const index_js_1 = require("../class/index.js");
function assertType(subject, validator) {
if (validator) {
if (((0, index_js_1.isConstructor)(validator) && !(subject instanceof validator)) || !validator(subject))
throw new TypeError(`subject fails to satisfy ${(0, tersify_1.tersify)(validator)}`);
}
return;
}
exports.assertType = assertType;
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 (!(0, index_js_1.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 ${(0, tersify_1.tersify)(validator)}`);
return;
};
};
assertType.as = function (_subject) { };
//# sourceMappingURL=assert_type.js.map