Function assert

A group of guard methods that assert their conditions and do nothing else.

This can also be called as a standalone assertion function which asserts that its input is truthy.

import {assert} from '@augment-vir/assert';

const value: unknown = 'some value' as unknown;
assert.isString(value);
// `value` will now be typed as a `string`

AssertionError When the assertion fails.

@augment-vir/assert

Properties

deepEquals: (<const Actual, const Expected>(actual: Actual, expected: Expected, failureMessage?: string) => asserts actual is NarrowToExpected<Actual, Expected>)

Asserts that two values are deeply equal using the deep-eql package.

Note that this check may be expensive, depending on what values it is passed. Whenever possible, use simpler equality checks instead (see the see section below).

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.deepEquals('a', 'a'); // passes

assert.deepEquals('1', 1); // fails

assert.deepEquals({a: 'a'}, {a: 'a'}); // passes

const objectExample = {a: 'a'};
assert.deepEquals(objectExample, objectExample); // passes

AssertionError If both inputs are not deeply equal.

endsWith: {
    <const ArrayElement>(parent: readonly ArrayElement[], child: ArrayElement, failureMessage?: string): void;
    (parent: string, child: string, failureMessage?: string): void;
    (parent: string | readonly string[], child: string, failureMessage?: string): void;
}

Asserts that a parent string or array ends with a specific child. This uses reference equality when the parent is an array.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.endsWith('ab', 'b'); // passes
assert.endsWith('ab', 'a'); // fails
assert.endsWith(
[
'a',
'b',
],
'b',
); // passes
assert.endsWith(
[
'a',
'b',
],
'a',
); // fails

AssertionError If the parent does not end with the child.

endsWithout: {
    <const ArrayElement>(parent: readonly ArrayElement[], child: ArrayElement, failureMessage?: string): void;
    (parent: string, child: string, failureMessage?: string): void;
    (parent: string | readonly string[], child: string, failureMessage?: string): void;
}

Asserts that a parent string or array does not end with a specific child. This uses reference equality when the parent is an array.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.endsWithout('ab', 'b'); // fails
assert.endsWithout('ab', 'a'); // passes
assert.endsWithout(
[
'a',
'b',
],
'b',
); // fails
assert.endsWithout(
[
'a',
'b',
],
'a',
); // passes

AssertionError If the parent ends with the child.

entriesEqual: (<const Actual, const Expected>(actual: Actual, expected: Expected, failureMessage?: string) => asserts actual is Expected)

Asserts that two objects are deeply equal by checking only their top-level values for strict (non-deep, reference, using ===) equality.

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.entriesEqual({a: 'a'}, {a: 'a'}); // passes

assert.entriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // fails

const bExample = {b: 'b'};
assert.entriesEqual({a: bExample}, {a: bExample}); // passes

AssertionError If both inputs are not equal.

fail: ((failureMessage?: string) => never) = ...

Immediately throw an assertion error.

import {assert} from '@augment-vir/assert';

assert.fail(); // throws an `AssertionError`

AssertionError

hasKey: (<const Key, const Parent>(parent: Parent, key: Key, failureMessage?: string) => asserts parent is CombineTypeWithKey<Key, Parent>)

Asserts that a parent value has the key.

Type guards the parent value.

Type declaration

    • <const Key, const Parent>(parent, key, failureMessage?): asserts parent is CombineTypeWithKey<Key, Parent>
    • Check if an object has the given property.

      Type Parameters

      • const Key extends PropertyKey
      • const Parent

      Parameters

      • parent: Parent
      • key: Key
      • OptionalfailureMessage: string

      Returns asserts parent is CombineTypeWithKey<Key, Parent>

import {assert} from '@augment-vir/assert';

assert.hasKey({a: 0, b: 1}, 'a'); // passes
assert.hasKey({a: 0, b: 1}, 'c'); // fails

AssertionError If the parent does not have the key.

hasKeys: (<const Keys, const Parent>(parent: Parent, keys: readonly Keys[], failureMessage?: string) => asserts parent is CombineTypeWithKey<Keys, Parent>)

Asserts that a parent value has all the keys.

Type guards the parent value.

Type declaration

    • <const Keys, const Parent>(parent, keys, failureMessage?): asserts parent is CombineTypeWithKey<Keys, Parent>
    • Check if an object has all the given properties.

      Type Parameters

      • const Keys extends PropertyKey
      • const Parent

      Parameters

      • parent: Parent
      • keys: readonly Keys[]
      • OptionalfailureMessage: string

      Returns asserts parent is CombineTypeWithKey<Keys, Parent>

import {assert} from '@augment-vir/assert';

assert.hasKeys({a: 0, b: 1}, [
'a',
'b',
]); // passes
assert.hasKeys({a: 0, b: 1}, [
'b',
'c',
]); // fails

AssertionError If the parent does not have all the keys.

hasValue: ((parent: object, value: unknown, failureMessage?: string) => void)

Asserts that an object/array parent includes a child value through reference equality.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};

assert.hasValue({child}, child); // passes
assert.hasValue({child: {a: 'a'}}, child); // fails
assert.hasValue([child], child); // passes

AssertionError If the assertion fails.

hasValues: ((parent: object, values: readonly unknown[], failureMessage?: string) => void)

Asserts that an object/array parent includes all child values through reference equality.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};
const child2 = {b: 'b'};

assert.hasValues({child, child2}, [
child,
child2,
]); // passes
assert.hasValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // fails
assert.hasValues(
[child],
[
child,
child2,
],
); // passes

AssertionError If the assertion fails.

instanceOf: (<const Instance>(instance: unknown, constructor: Constructor<Instance>, failureMessage?: string) => asserts instance is Instance)

Asserts that a value is an instance of the given class constructor.

Type guards the value.

Type declaration

    • <const Instance>(instance, constructor, failureMessage?): asserts instance is Instance
    • Wraps the JavaScript built-in "instanceof" in a type guard assertion.

      Type Parameters

      • const Instance

      Parameters

      • instance: unknown
      • constructor: Constructor<Instance>

        The constructor that the "instance" input will be checked against.

      • OptionalfailureMessage: string

        Message to include in error message if this assertion fails.

      Returns asserts instance is Instance

import {assert} from '@augment-vir/assert';

assert.instanceOf(/abc/, RegExp); // passes
assert.instanceOf('abc', RegExp); // fails

AssertionError If the value is not an instance of the given class constructor.

isAbove: ((actual: number, expected: number, failureMessage?: string) => void)

Asserts that a number is above the expectation (actual > expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isAbove(10, 5); // passes
assert.isAbove(5, 5); // fails
assert.isAbove(5, 10); // fails

AssertionError If the assertion fails.

isApproximately: ((actual: number, expected: number, delta: number, failureMessage?: string) => void)

Asserts that a number is within ±delta of the expectation.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isApproximately(10, 8, 4); // passes
assert.isApproximately(10, 12, 4); // passes
assert.isApproximately(10, 8, 1); // fails
assert.isApproximately(10, 12, 1); // fails

AssertionError If the assertion fails.

isArray: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is ArrayNarrow<Actual>)

Asserts that a value is an array.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isArray([]); // passes
assert.isArray({length: 4}); // fails

AssertionError If the assertion failed.

isAtLeast: ((actual: number, expected: number, failureMessage?: string) => void)

Asserts that a number is at least the expectation (actual >= expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isAtLeast(10, 5); // passes
assert.isAtLeast(5, 5); // passes
assert.isAtLeast(5, 10); // fails

AssertionError If the assertion fails.

isAtMost: ((actual: number, expected: number, failureMessage?: string) => void)

Asserts that a number is at most the expectation (actual <= expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isAtMost(10, 5); // fails
assert.isAtMost(5, 5); // passes
assert.isAtMost(5, 10); // passes

AssertionError If the assertion fails.

isBelow: ((actual: number, expected: number, failureMessage?: string) => void)

Asserts that a number is below the expectation (actual < expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isBelow(10, 5); // fails
assert.isBelow(5, 5); // fails
assert.isBelow(5, 10); // passes

AssertionError If the assertion fails.

isBigInt: ((actual: unknown, failureMessage?: string) => asserts actual is bigint)

Asserts that a value is a BigInt.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isBigInt(123n); // passes
assert.isBigInt(123); // fails

AssertionError If the assertion failed.

isBoolean: ((actual: unknown, failureMessage?: string) => asserts actual is boolean)

Asserts that a value is a boolean.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isBoolean(true); // passes
assert.isBoolean('true'); // fails

AssertionError If the assertion failed.

isDefined: (<const Actual>(input: Actual, failureMessage?: string) => asserts input is Exclude<Actual, undefined | null>)

Asserts that a value is defined (not null and not undefined).

Type guards the value.

Type declaration

    • <const Actual>(input, failureMessage?): asserts input is Exclude<Actual, undefined | null>
    • Type Parameters

      • const Actual

      Parameters

      • input: Actual

        The value to check.

      • OptionalfailureMessage: string

        Message to include in error message if this assertion fails.

      Returns asserts input is Exclude<Actual, undefined | null>

import {assert} from '@augment-vir/assert';

assert.isDefined(0); // passes
assert.isDefined(''); // passes
assert.isDefined(null); // fails
assert.isDefined(undefined); // fails

AssertionError If the assertion fails.

isEmpty: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is NarrowToActual<Actual, Empty>)

Asserts that a value is empty. Supports strings, Maps, Sets, objects, and arrays.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isEmpty({}); // passes
assert.isEmpty(''); // passes
assert.isEmpty([]); // passes

assert.isEmpty('a'); // fails
assert.isEmpty({a: 'a'}); // fails

AssertionError If the assertion fails.

isEnumValue: (<const Expected>(child: unknown, checkEnum: Expected, failureMessage?: string) => asserts child is Expected[keyof Expected])

Asserts that a child value is an enum member.

Type guards the child value.

import {assert} from '@augment-vir/assert';

enum MyEnum {
A = 'a',
B = 'b',
}

assert.isEnumValue('a', MyEnum); // passes
assert.isEnumValue('A', MyEnum); // fails

AssertionError If the child is not an enum member.

isError: ((actual: unknown, matchOptions?: PartialWithNullable<{
    matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
    matchMessage: string | RegExp;
}>, failureMessage?: string) => asserts actual is Error)

Asserts that a value is an instance of the built-in Error class and compares it to the given ErrorMatchOptions, if provided.

Type guards the input.

import {assert} from '@augment-vir/assert';

assert.isError(new Error()); // passes
assert.isError(new Error(), {matchMessage: 'hi'}); // fails
assert.isError({message: 'not an error'}); // fails

AssertionError If the assertion fails.

isFalse: ((input: unknown, failureMessage?: string) => asserts input is false)

Asserts that a value is exactly false.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isFalse(true); // fails
assert.isFalse(false); // passes
assert.isFalse(1); // fails
assert.isFalse(0); // fails

AssertionError If the value is not false.

isFalsy: ((input: unknown, failureMessage?: string) => asserts input is FalsyValue)

Asserts that a value is falsy.

Type guards the value when possible.

import {assert} from '@augment-vir/assert';

assert.isFalsy(true); // fails
assert.isFalsy(false); // passes
assert.isFalsy(1); // fails
assert.isFalsy(0); // passes

AssertionError If the value is not falsy.

isFinite: ((actual: number, failureMessage?: string) => void)

Asserts that a number is finite: meaning, not NaN and not Infinity or -Infinity.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isFinite(10); // passes
assert.isFinite(parseInt('invalid')); // fails
assert.isFinite(Infinity); // fails
assert.isFinite(-Infinity); // fails

AssertionError If the assertion fails.

isFunction: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is NarrowToActual<Actual, AnyFunction>)

Asserts that a value is a function.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isFunction(() => {}); // passes
assert.isFunction({}); // fails

AssertionError If the assertion failed.

isHttpStatus: ((actual: unknown, failureMessage?: string) => asserts actual is HttpStatus)

Asserts that a value is an HttpStatus.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isHttpStatus(400); // passes
assert.isHttpStatus(500); // passes
assert.isHttpStatus(99); // fails

AssertionError If the value is not an HttpStatus.

isHttpStatusCategory: (<const Actual, const Category>(actual: Actual, category: Category, failureMessage?: string) => asserts actual is NarrowToExpected<Actual, HttpStatusByCategory<Category>>)

Asserts that a value is an HttpStatus within a specific HttpStatusCategory.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isHttpStatusCategory(400, HttpStatusCategory.ClientError); // passes
assert.isHttpStatusCategory(500, HttpStatusCategory.Success); // fails
assert.isHttpStatusCategory(99, HttpStatusCategory.Information); // fails

AssertionError If the value is not an HttpStatus within the HttpStatusCategory.

isIn: (<const Parent>(child: unknown, parent: Parent, failureMessage?: string) => asserts child is Values<Parent>)

Asserts that child value is contained within a parent object, array, or string through reference equality.

Type guards the child when possible.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};

assert.isIn(child, {child}); // passes
assert.isIn('a', 'ab'); // passes
assert.isIn(child, [child]); // passes

assert.isIn(child, {child: {a: 'a'}}); // fails
assert.isIn('a', 'bc'); // fails

AssertionError If the assertion fails.

isInfinite: ((actual: number, failureMessage?: string) => void)

Asserts that a number is either Infinity or -Infinity.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isInfinite(10); // fails
assert.isInfinite(parseInt('invalid')); // fails
assert.isInfinite(Infinity); // passes
assert.isInfinite(-Infinity); // passes

AssertionError If the assertion fails.

isKeyOf: (<const Parent>(key: PropertyKey, parent: Parent, failureMessage?: string) => asserts key is keyof Parent)

Asserts that a key is contained within a parent value.

Type guards the key.

import {assert} from '@augment-vir/assert';

assert.isKeyof('a', {a: 0, b: 1}); // passes
assert.isKeyof('c', {a: 0, b: 1}); // fails

AssertionError If the key is not in the parent.

isLengthAtLeast: {
    <const Element, const Length>(actual: readonly (undefined | Element)[], length: Length, failureMessage?: string): asserts actual is readonly [Tuple<Element, Length>, Element];
    (actual: string | AnyObject, length: number, failureMessage?: string): void;
}

Asserts that an array or string has at least the given length.

Type guards an array into an AtLeastTuple. Performs no type guarding on a string.

import {assert} from '@augment-vir/assert';

assert.isLengthAtLeast(
[
'a',
'b',
'c',
],
2,
); // passes
assert.isLengthAtLeast(
[
'a',
'b',
'c',
],
3,
); // passes
assert.isLengthAtLeast(
[
'a',
'b',
],
3,
); // fails

AssertionError If the value is less than the given length.

isLengthExactly: {
    <const Element, const Length>(actual: readonly (undefined | Element)[], length: Length, failureMessage?: string): asserts actual is Tuple<Element, Length>;
    (actual: string | AnyObject, length: number, failureMessage?: string): void;
}

Asserts that an array or string has exactly the given length.

Type guards an array into a Tuple. Performs no type guarding on a string.

import {assert} from '@augment-vir/assert';

assert.isLengthExactly(
[
'a',
'b',
'c',
],
2,
); // fails
assert.isLengthExactly(
[
'a',
'b',
'c',
],
3,
); // passes
assert.isLengthExactly(
[
'a',
'b',
],
3,
); // fails

AssertionError If the value is not exactly the given length.

isNaN: ((actual: number, failureMessage?: string) => void)

Asserts that a number is NaN.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isNaN(10); // fails
assert.isNaN(parseInt('invalid')); // passes
assert.isNaN(Infinity); // fails

AssertionError If the assertion fails.

isNotApproximately: ((actual: number, expected: number, delta: number, failureMessage?: string) => void)

Asserts that a number is outside ±delta of the expectation.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.isNotApproximately(10, 8, 4); // fails
assert.isNotApproximately(10, 12, 4); // fails
assert.isNotApproximately(10, 8, 1); // passes
assert.isNotApproximately(10, 12, 1); // passes

AssertionError If the assertion fails.

isNotArray: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, readonly unknown[]>)

Asserts that a value is not an array.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotArray([]); // fails
assert.isNotArray({length: 4}); // passes

AssertionError If the assertion failed.

isNotBigInt: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, bigint>)

Asserts that a value is not a BigInt.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotBigInt(123n); // fails
assert.isNotBigInt(123); // passes

AssertionError If the assertion failed.

isNotBoolean: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, boolean>)

Asserts that a value is not a boolean.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotBoolean(true); // fails
assert.isNotBoolean('true'); // passes

AssertionError If the assertion failed.

isNotEmpty: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, Empty>)

Asserts that a value is not empty. Supports strings, Maps, Sets, objects, and arrays.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotEmpty({}); // fails
assert.isNotEmpty(''); // fails
assert.isNotEmpty([]); // fails

assert.isNotEmpty('a'); // passes
assert.isNotEmpty({a: 'a'}); // passes

AssertionError If the assertion fails.

isNotEnumValue: (<const Actual, const Expected>(child: Actual, checkEnum: Expected, failureMessage?: string) => asserts child is Exclude<Actual, Expected[keyof Expected] | `${Expected[keyof Expected]}`>)

Asserts that a child value is not an enum member.

Type guards the child value.

import {assert} from '@augment-vir/assert';

enum MyEnum {
A = 'a',
B = 'b',
}

assert.isNotEnumValue('a', MyEnum); // fails
assert.isNotEnumValue('A', MyEnum); // passes

AssertionError If the child is an enum member.

isNotFunction: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, AnyFunction>)

Asserts that a value is not a function.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotFunction(() => {}); // fails
assert.isNotFunction({}); // passes

AssertionError If the assertion failed.

isNotIn: (<const Parent, const Child>(child: Child, parent: Parent, failureMessage?: string) => asserts child is Exclude<Child, Values<Parent>>)

Asserts that child value is not contained within a parent object, array, or string through reference equality.

Type guards the child when possible.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};

assert.isNotIn(child, {child}); // fails
assert.isNotIn('a', 'ab'); // fails
assert.isNotIn(child, [child]); // fails

assert.isNotIn(child, {child: {a: 'a'}}); // passes
assert.isNotIn('a', 'bc'); // passes

AssertionError If the assertion fails.

isNotKeyOf: (<const Key, const Parent>(key: Key, parent: Parent, failureMessage?: string) => asserts key is Exclude<Key, RequiredKeysOf<Parent>>)

Asserts that a key is not contained within a parent value.

Type guards the key.

import {assert} from '@augment-vir/assert';

assert.isNotKeyOf('a', {a: 0, b: 1}); // fails
assert.isNotKeyOf('c', {a: 0, b: 1}); // passes

AssertionError If the key is in the parent.

isNotNull: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, null>)

Asserts that a value is not exactly null.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotNull(null); // fails
assert.isNotNull(undefined); // passes

AssertionError If the assertion failed.

isNotNumber: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, number>)

Asserts that a value is not a number. This includes NaN.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotNumber(123); // fails
assert.isNotNumber(123n); // passes

AssertionError If the assertion failed.

isNotObject: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, UnknownObject>)

Asserts that a value is not an object. This includes arrays.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotObject({}); // fails
assert.isNotObject([]); // passes

AssertionError If the assertion failed.

isNotPrimitive: (<const Actual>(input: Actual, failureMessage?: string) => asserts input is Exclude<Actual, Primitive>)

Asserts that a value is not a JavaScript primitive.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isPrimitive('key'); // fails
assert.isPrimitive(true); // fails
assert.isPrimitive({}); // passes

AssertionError If the assertion fails.

isNotPromise: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, Promise<any>>)

Asserts that a value is a not Promise instance.

Type guards the value.

import {assert} from '@augment-vir/assert';

class CustomThenable {
constructor(public value: any) {}

then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}

assert.isNotPromise(Promise.resolve(5)); // fails
assert.isNotPromise(new CustomThenable(5)); // passes
assert.isNotPromise(5); // passes

AssertionError If the assertion fails.

isNotPromiseLike: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, PromiseLike<any>>)

Asserts that a value is not a PromiseLike.

PromiseLike is TypeScript built-in type that has a .then method and thus behaves like a promise. This is also referred to as a "thenable". This enables the use of third-party promise implementations that aren't instances of the built-in Promise class.

Type guards the value.

import {assert} from '@augment-vir/assert';

class CustomThenable {
constructor(public value: any) {}

then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}

assert.isNotPromiseLike(Promise.resolve(5)); // fails
assert.isNotPromiseLike(new CustomThenable(5)); // fails
assert.isNotPromiseLike(5); // passes

AssertionError If the assertion fails.

isNotPropertyKey: (<const Actual>(input: Actual, failureMessage?: string) => asserts input is Exclude<Actual, PropertyKey>)

Asserts that a value is not a valid PropertyKey. PropertyKey is a built-in TypeScript type which refers to all possible key types for a JavaScript object.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotPropertyKey('key'); // fails
assert.isNotPropertyKey(true); // passes
assert.isNotPropertyKey({}); // passes

AssertionError If the assertion fails.

isNotString: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, string>)

Asserts that a value is not a string.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotString(''); // fails
assert.isNotString(5); // passes

AssertionError If the assertion failed.

isNotSymbol: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, symbol>)

Asserts that a value is not a symbol.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotSymbol(Symbol('my-symbol')); // fails
assert.isNotSymbol('my-symbol'); // passes

AssertionError If the assertion failed.

isNotUndefined: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, undefined>)

Asserts that a value is not exactly undefined.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNotUndefined(undefined); // fails
assert.isNotUndefined(null); // passes

AssertionError If the assertion failed.

isNotUuid: (<const Actual>(actual: Actual, failureMessage?: string) => asserts actual is Exclude<Actual, `${string}-${string}-${string}-${string}-${string}`>)

Asserts that a value is not a valid UUID. The nil or max UUIDs are included as not valid.

Type guards the value.

import {assert} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';

assert.isNotUuid(createUuidV4()); // fails
assert.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // fails
assert.isNotUuid('00000000-0000-0000-0000-000000000000'); // passes
assert.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // passes
assert.isNotUuid('not-a-uuid'); // passes

AssertionError If the assertion fails.

isNull: ((actual: unknown, failureMessage?: string) => asserts actual is null)

Asserts that a value is exactly null.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNull(null); // passes
assert.isNull(undefined); // fails

AssertionError If the assertion failed.

isNullish: ((input: unknown, failureMessage?: string) => asserts input is undefined | null)

Asserts that a value is nullish (null or undefined).

Type guards the value.

Type declaration

    • (input, failureMessage?): asserts input is undefined | null
    • Parameters

      • input: unknown

        The value to check.

      • OptionalfailureMessage: string

        Message to include in error message if this assertion fails.

      Returns asserts input is undefined | null

import {assert} from '@augment-vir/assert';

assert.isNullish(0); // fails
assert.isNullish(''); // fails
assert.isNullish(null); // passes
assert.isNullish(undefined); // passes

AssertionError If the assertion fails.

isNumber: ((actual: unknown, failureMessage?: string) => asserts actual is number)

Asserts that a value is a number. This excludes NaN.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isNumber(123); // passes
assert.isNumber(123n); // fails

AssertionError If the assertion failed.

isObject: ((actual: unknown, failureMessage?: string) => asserts actual is UnknownObject)

Asserts that a value is an object. This excludes arrays.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isObject({}); // passes
assert.isObject([]); // fails

AssertionError If the assertion failed.

isPrimitive: ((input: unknown, failureMessage?: string) => asserts input is Primitive)

Asserts that a value is a JavaScript primitive.

Type guards the value.

Type declaration

    • (input, failureMessage?): asserts input is Primitive
    • Asserts that the given value is a primitive.

      Parameters

      • input: unknown
      • OptionalfailureMessage: string

      Returns asserts input is Primitive

import {assert} from '@augment-vir/assert';

assert.isPrimitive('key'); // passes
assert.isPrimitive(true); // passes
assert.isPrimitive({}); // fails

AssertionError If the assertion fails.

isPromise: ((actual: unknown, failureMessage?: string) => asserts actual is Promise<any>)

Asserts that a value is a Promise instance.

Type guards the value.

Type declaration

    • (actual, failureMessage?): asserts actual is Promise<any>
    • Checks if a value is an actual Promise object. In reality this is just a simple wrapper for instanceof Promise, but it makes checking a bit more ergonomic.

      Parameters

      • actual: unknown
      • OptionalfailureMessage: string

      Returns asserts actual is Promise<any>

import {assert} from '@augment-vir/assert';

class CustomThenable {
constructor(public value: any) {}

then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}

assert.isPromise(Promise.resolve(5)); // passes
assert.isPromise(new CustomThenable(5)); // fails
assert.isPromise(5); // fails

AssertionError If the assertion fails.

isPromiseLike: ((actual: unknown, failureMessage?: string) => asserts actual is PromiseLike<any>)

Asserts that a value is a PromiseLike.

PromiseLike is TypeScript built-in type that has a .then method and thus behaves like a promise. This is also referred to as a "thenable". This enables the use of third-party promise implementations that aren't instances of the built-in Promise class.

Type guards the value.

import {assert} from '@augment-vir/assert';

class CustomThenable {
constructor(public value: any) {}

then(onFulfilled?: AnyFunction, onRejected?: AnyFunction) {
return new CustomThenable(onFulfilled ? onFulfilled(this.value) : this.value);
}
}

assert.isPromiseLike(Promise.resolve(5)); // passes
assert.isPromiseLike(new CustomThenable(5)); // passes
assert.isPromiseLike(5); // fails

AssertionError If the assertion fails.

isPropertyKey: ((input: unknown, failureMessage?: string) => asserts input is PropertyKey)

Asserts that a value is a valid PropertyKey. PropertyKey is a built-in TypeScript type which refers to all possible key types for a JavaScript object.

Type guards the value.

Type declaration

    • (input, failureMessage?): asserts input is PropertyKey
    • Asserts that the given value is a PropertyKey ( string | number | symbol).

      Parameters

      • input: unknown
      • OptionalfailureMessage: string

      Returns asserts input is PropertyKey

import {assert} from '@augment-vir/assert';

assert.isPropertyKey('key'); // passes
assert.isPropertyKey(true); // fails
assert.isPropertyKey({}); // fails

AssertionError If the assertion fails.

isString: ((actual: unknown, failureMessage?: string) => asserts actual is string)

Asserts that a value is a string.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isString(''); // passes
assert.isString(5); // fails

AssertionError If the assertion failed.

isSymbol: ((actual: unknown, failureMessage?: string) => asserts actual is symbol)

Asserts that a value is a symbol.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isSymbol(Symbol('my-symbol')); // passes
assert.isSymbol('my-symbol'); // fails

AssertionError If the assertion failed.

isTrue: ((input: unknown, failureMessage?: string) => asserts input is true)

Asserts that a value is exactly true.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isTrue(true); // passes
assert.isTrue(false); // fails
assert.isTrue(1); // fails
assert.isTrue(0); // fails

AssertionError If the value is not true.

isTruthy: (<const Actual>(input: Actual, failureMessage?: string) => asserts input is Exclude<Actual, FalsyValue>)

Asserts that a value is truthy.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isTruthy(true); // passes
assert.isTruthy(false); // fails
assert.isTruthy(1); // passes
assert.isTruthy(0); // fails

AssertionError If the value is not truthy.

isUndefined: ((actual: unknown, failureMessage?: string) => asserts actual is undefined)

Asserts that a value is exactly undefined.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.isUndefined(undefined); // passes
assert.isUndefined(null); // fails

AssertionError If the assertion failed.

isUuid: ((actual: unknown, failureMessage?: string) => asserts actual is `${string}-${string}-${string}-${string}-${string}`)

Asserts that a value is a valid UUID. Does not accept the nil or max UUIDs.

Type guards the value.

Type declaration

    • (actual, failureMessage?): asserts actual is `${string}-${string}-${string}-${string}-${string}`
    • Checks if the input string is a valid v4 UUID.

      Parameters

      • actual: unknown
      • OptionalfailureMessage: string

      Returns asserts actual is `${string}-${string}-${string}-${string}-${string}`

import {assert} from '@augment-vir/assert';
import {createUuidV4} from '@augment-vir/common';

assert.isUuid(createUuidV4()); // passes
assert.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // passes
assert.isUuid('00000000-0000-0000-0000-000000000000'); // fails
assert.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // fails
assert.isUuid('not-a-uuid'); // fails

AssertionError If the assertion fails.

jsonEquals: (<const Actual, const Expected>(actual: Actual, expected: Expected, failureMessage?: string) => asserts actual is Expected)

Asserts that two values are deeply equal when stringified into JSON. This will fail or may not make any sense if the values are not valid JSON. This internally sorts all given object keys so it is insensitive to object key order.

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.jsonEquals({a: 'a'}, {a: 'a'}); // passes

assert.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // fails

AssertionError If both inputs are not equal.

lacksKey: (<const Parent, const Key>(parent: Parent, key: Key, failureMessage?: string) => asserts parent is Exclude<Parent, Record<Key, any>>)

Asserts that a parent value does not have the key.

Type guards the parent value.

import {assert} from '@augment-vir/assert';

assert.lacksKey({a: 0, b: 1}, 'a'); // fails
assert.lacksKey({a: 0, b: 1}, 'c'); // passes

AssertionError If the parent has the key.

lacksKeys: (<const Parent, const Key>(parent: Parent, keys: readonly Key[], failureMessage?: string) => asserts parent is Exclude<Parent, Partial<Record<Key, any>>>)

Asserts that a parent value none of the keys.

Type guards the parent value.

import {assert} from '@augment-vir/assert';

assert.lacksKeys({a: 0, b: 1}, [
'b',
'c',
]); // fails
assert.lacksKeys({a: 0, b: 1}, [
'c',
'd',
]); // passes

AssertionError If the parent has any of the keys.

lacksValue: ((parent: object, value: unknown, failureMessage?: string) => void)

Asserts that an object/array parent does not include a child value through reference equality.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};

assert.lacksValue({child}, child); // fails
assert.lacksValue({child: {a: 'a'}}, child); // passes
assert.lacksValue([child], child); // fails

AssertionError If the assertion fails.

lacksValues: ((parent: object, values: readonly unknown[], failureMessage?: string) => void)

Asserts that an object/array parent includes none of the provided child values through reference equality.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

const child = {a: 'a'};
const child2 = {b: 'b'};

assert.lacksValues({}, [
child,
child2,
]); // passes
assert.lacksValues({child, child2}, [
child,
child2,
]); // fails
assert.lacksValues({child: {a: 'a'}, child2}, [
child,
child2,
]); // fails

AssertionError If the assertion fails.

looseEquals: ((actual: unknown, expected: unknown, failureMessage?: string) => void)

Asserts that two values are loosely equal (using ==).

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.looseEquals('a', 'a'); // passes

assert.looseEquals('1', 1); // passes

assert.looseEquals({a: 'a'}, {a: 'a'}); // fails

const objectExample = {a: 'a'};
assert.looseEquals(objectExample, objectExample); // passes

AssertionError If both inputs are not loosely equal.

matches: ((actual: string, expected: RegExp, failureMessage?: string) => void)

Asserts that a string (first input, actual) matches a RegExp (second input, expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.matches('hi', /^h/); // passes
assert.matches('hi', /^g/); // fails

AssertionError If the assertion fails.

mismatches: ((actual: string, expected: RegExp, failureMessage?: string) => void)

Asserts that a string (first input, actual) does not match a RegExp (second input, expected).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.mismatches('hi', /^h/); // fails
assert.mismatches('hi', /^g/); // passes

AssertionError If the assertion fails.

notDeepEquals: ((actual: unknown, expected: unknown, failureMessage?: string) => void)

Asserts that two values are not deeply equal using the deep-eql package.

Note that this check may be expensive, depending on what values it is passed. Whenever possible, use simpler equality checks instead (see the see section below).

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.deepEquals('a', 'a'); // false

assert.deepEquals('1', 1); // passes

assert.deepEquals({a: 'a'}, {a: 'a'}); // fails

const objectExample = {a: 'a'};
assert.deepEquals(objectExample, objectExample); // fails

AssertionError If both inputs are deeply equal.

notEntriesEqual: ((actual: object, expected: object, failureMessage?: string) => void)

Asserts that two objects are not deeply equal by checking only their top-level values for strict (non-deep, reference, using ===) equality.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.notEntriesEqual({a: 'a'}, {a: 'a'}); // fails

assert.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // passes

const bExample = {b: 'b'};
assert.notEntriesEqual({a: bExample}, {a: bExample}); // fails

AssertionError If both inputs are equal.

notInstanceOf: (<const Actual, const Instance>(instance: Actual, constructor: Constructor<Instance>, failureMessage?: string) => asserts instance is Exclude<Actual, Instance>)

Asserts that a value is not an instance of the given class constructor.

Type guards the value.

import {assert} from '@augment-vir/assert';

assert.notInstanceOf(/abc/, RegExp); // fails
assert.notInstanceOf('abc', RegExp); // passes

AssertionError If the value is an instance of the given class constructor.

notJsonEquals: ((actual: unknown, expected: unknown, failureMessage?: string) => void)

Asserts that two values are not deeply equal when stringified into JSON. This may not make any sense if the values are not valid JSON. This internally sorts all given object keys so it is insensitive to object key order.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.notJsonEquals({a: 'a'}, {a: 'a'}); // fails

assert.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // passes

AssertionError If both inputs are not equal.

notLooseEquals: ((actual: unknown, expected: unknown, failureMessage?: string) => void)

Asserts that two values are not loosely equal (using ==).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.notLooseEquals('a', 'a'); // fails

assert.notLooseEquals('1', 1); // fails

assert.notLooseEquals({a: 'a'}, {a: 'a'}); // passes

const objectExample = {a: 'a'};
assert.notLooseEquals(objectExample, objectExample); // fails

AssertionError If both inputs are loosely equal.

notStrictEquals: ((actual: unknown, expected: unknown, failureMessage?: string) => void)

Asserts that two values are not strictly equal (using ===).

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.notStrictEquals('a', 'a'); // fails

assert.notStrictEquals('1', 1); // passes

assert.notStrictEquals({a: 'a'}, {a: 'a'}); // passes

const objectExample = {a: 'a'};
assert.notStrictEquals(objectExample, objectExample); // fails

AssertionError If both inputs are strictly equal.

output: {
    <const FunctionToCall>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string): OutputReturn<NoInfer<FunctionToCall>, void>;
    <const FunctionToCall>(asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>, functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string): OutputReturn<NoInfer<FunctionToCall>, void>;
}

Asserts that the output of the given function deeply equals expectations. A custom asserter can optionally be provided as the first argument to change the expectation checking from "deeply equals" to whatever you want.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.output((input: number) => String(input), [5], '5'); // passes
assert.output((input: number) => String(input), [10], '5'); // fails

assert.output(assert.isLengthAtLeast, (input: number) => String(input), [5], 2); // fails
assert.output(assert.isLengthAtLeast, (input: number) => String(input), [10], 2); // passes

AssertionError If the assertion fails.

startsWith: {
    <const ArrayElement>(parent: readonly ArrayElement[], child: ArrayElement, failureMessage?: string): void;
    (parent: string, child: string, failureMessage?: string): void;
    (parent: string | readonly string[], child: string, failureMessage?: string): void;
}

Asserts that a parent string or array starts with a specific child. This uses reference equality when the parent is an array.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.startsWith('ab', 'b'); // fails
assert.startsWith('ab', 'a'); // passes
assert.startsWith(
[
'a',
'b',
],
'b',
); // fails
assert.startsWith(
[
'a',
'b',
],
'a',
); // passes

AssertionError If the parent does not start with the child.

startsWithout: {
    <const ArrayElement>(parent: readonly ArrayElement[], child: ArrayElement, failureMessage?: string): void;
    (parent: string, child: string, failureMessage?: string): void;
    (parent: string | readonly string[], child: string, failureMessage?: string): void;
}

Asserts that a parent string or array starts with a specific child. This uses reference equality when the parent is an array.

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.startsWith('ab', 'b'); // passes
assert.startsWith('ab', 'a'); // fails
assert.startsWith(
[
'a',
'b',
],
'b',
); // passes
assert.startsWith(
[
'a',
'b',
],
'a',
); // fails

AssertionError If the parent does start with the child.

strictEquals: (<const Actual, const Expected>(actual: Actual, expected: Expected, failureMessage?: string) => asserts actual is Expected)

Asserts that two values are strictly equal (using ===).

Type guards the first value.

import {assert} from '@augment-vir/assert';

assert.strictEquals('a', 'a'); // passes

assert.strictEquals('1', 1); // fails

assert.strictEquals({a: 'a'}, {a: 'a'}); // fails

const objectExample = {a: 'a'};
assert.strictEquals(objectExample, objectExample); // passes

AssertionError If both inputs are not strictly equal.

throws: {
    (callbackOrPromise: (() => never), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>, failureMessage?: string): void;
    (callbackOrPromise: Promise<any> | (() => Promise<any>), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>, failureMessage?: string): Promise<void>;
    (callback: (() => any), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>, failureMessage?: string): void;
    (callback: Promise<any> | (() => any), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>, failureMessage?: string): MaybePromise<void>;
}

If a function input is provided:

Calls that function and asserts that the function throw an error, comparing the error with the given ErrorMatchOptions, if provided.

If a promise is provided:

Awaits the promise and asserts that the promise rejected with an error, comparing the error with the given ErrorMatchOptions, if provided.

This assertion will automatically type itself as async vs async based on the input. (A promise or async function inputs results in async. Otherwise, sync.)

Performs no type guarding.

import {assert} from '@augment-vir/assert';

assert.throws(() => {
throw new Error();
}); // passes
assert.throws(
() => {
throw new Error();
},
{matchMessage: 'hi'},
); // fails
await assert.throws(Promise.reject()); // passes
assert.throws(() => {}); // fails

AssertionError If the assertion fails.

tsType: {
    <Actual>(input: Actual): AssertTypeOf<Actual>;
    <Actual>(): AssertTypeOf<Actual>;
}

Asserts within the TypeScript type system that a given type or value matches type expectations (using the expect-type package. Make sure to call a method on the first call to actually assert anything.

Use this to write type tests. Don't use this in production code. It won't cause issues, but it's a useless no-op at run-time (it's not even a type guard).

Performs no type guarding.

Type declaration

    • <Actual>(input): AssertTypeOf<Actual>
    • Type Parameters

      • Actual

      Parameters

      • input: Actual

        Run-time value to type check.

      Returns AssertTypeOf<Actual>

    • <Actual>(): AssertTypeOf<Actual>
    • Uses the expect-type package to assert type matching.

      Type Parameters

      • Actual

      Returns AssertTypeOf<Actual>

import {assert} from '@augment-vir/assert';

assert.tsType('hello').equals<string>();
assert.tsType<'hello>().equals<'hello'>();
assert.tsType<'hello>().notEquals<string>();
assert.tsType('hello').notEquals<number>();

Never returns anything.

Never throws anything.