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.

Properties

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.

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

Immediately throw an assertion error.

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

assert.fail(); // throws an `AssertionError`
isError: (
    this: void,
    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.

isLengthAtLeast: {
    <const Element, const Length extends number>(
        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 extends number>(
        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.

output: {
    <const FunctionToCall extends AnyFunction>(
        functionToCall: FunctionToCall,
        inputs: Parameters<NoInfer<FunctionToCall>>,
        expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>,
        failureMessage?: string,
    ): OutputReturn<NoInfer<FunctionToCall>, void>;
    <const FunctionToCall extends AnyFunction>(
        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.

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

Methods

  • 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.

    Type Parameters

    • const Actual
    • const Expected

    Parameters

    • this: void
    • actual: Actual
    • expected: Expected
    • OptionalfailureMessage: string

    Returns asserts actual is NarrowToExpected<Actual, Expected>

    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.

  • Asserts that a parent value has the key.

    Type guards the parent value.

    Type Parameters

    • const Key extends PropertyKey
    • const Parent

    Parameters

    • this: void
    • 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.

  • Asserts that a parent value has all the keys.

    Type guards the parent value.

    Type Parameters

    • const Keys extends PropertyKey
    • const Parent

    Parameters

    • this: void
    • 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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • parent: string | object
    • value: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • parent: string | object
    • values: unknown[]
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Type guards the value.

    Type Parameters

    • const Instance

    Parameters

    • this: void
    • 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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • delta: number
    • OptionalfailureMessage: string

    Returns void

    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.

  • Asserts that a value is an array.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is ArrayNarrow<Actual>

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

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

    AssertionError If the assertion failed.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • OptionalfailureMessage: string

    Returns void

    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.

  • Asserts that a value is a BigInt.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is bigint

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is a boolean.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is boolean

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Type Parameters

    • const Actual

    Parameters

    • this: void
    • 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.

  • Asserts that a child value is an enum member.

    Type guards the child value.

    Type Parameters

    Parameters

    • this: void
    • child: unknown
    • checkEnum: Expected
    • OptionalfailureMessage: string

    Returns asserts child is Expected[keyof Expected]

    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.

  • Asserts that a value is exactly false.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown
    • OptionalfailureMessage: string

    Returns asserts input is false

    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.

  • Asserts that a value is falsy.

    Type guards the value when possible.

    Parameters

    • this: void
    • input: unknown
    • OptionalfailureMessage: string

    Returns asserts input is FalsyValue

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Type guards the child when possible.

    Type Parameters

    • Parent extends string | object

    Parameters

    • this: void
    • child: unknown
    • parent: Parent
    • OptionalfailureMessage: string

    Returns asserts child is Values<Parent>

    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.

  • Asserts that a number is inside the provided min and max bounds, inclusive.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • __namedParameters: MinMax
    • OptionalfailureMessage: string

    Returns void

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

    assert.isInBounds(5, {min: 1, max: 10}); // passes
    assert.isInBounds(10, {min: 1, max: 10}); // passes
    assert.isInBounds(11, {min: 1, max: 10}); // fails
    assert.isInBounds(0, {min: 1, max: 10}); // fails

    AssertionError If the assertion fails.

  • Asserts that a number is either Infinity or -Infinity.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Type guards the key.

    Type Parameters

    • const Parent

    Parameters

    • this: void
    • key: PropertyKey
    • parent: Parent
    • OptionalfailureMessage: string

    Returns asserts key is keyof Parent

    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.

  • Asserts that a number is NaN.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • OptionalfailureMessage: string

    Returns void

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

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

    AssertionError If the assertion fails.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number
    • delta: number
    • OptionalfailureMessage: string

    Returns void

    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.

  • Asserts that a value is not an array.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, readonly unknown[]>

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is not a BigInt.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, bigint>

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is not a boolean.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, boolean>

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Type Parameters

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, Empty>

    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.

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

    Type guards the child when possible.

    Type Parameters

    • Parent extends string | object
    • Child

    Parameters

    • this: void
    • child: Child
    • parent: Parent
    • OptionalfailureMessage: string

    Returns asserts child is Exclude<Child, Values<Parent>>

    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.

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

    Type guards the key.

    Type Parameters

    • const Key extends PropertyKey
    • const Parent

    Parameters

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

    Returns asserts key is Exclude<Key, RequiredKeysOf<Parent>>

    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.

  • Asserts that a value is not exactly null.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, null>

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, number>

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is not a JavaScript primitive.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, Primitive>

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

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

    AssertionError If the assertion fails.

  • Asserts that a value is a not Promise instance.

    Type guards the value.

    Type Parameters

    • const Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, 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.isNotPromise(Promise.resolve(5)); // fails
    assert.isNotPromise(new CustomThenable(5)); // passes
    assert.isNotPromise(5); // passes

    AssertionError If the assertion fails.

  • 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.

    Type Parameters

    • const Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, PromiseLike<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.isNotPromiseLike(Promise.resolve(5)); // fails
    assert.isNotPromiseLike(new CustomThenable(5)); // fails
    assert.isNotPromiseLike(5); // passes

    AssertionError If the assertion fails.

  • 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.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, PropertyKey>

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

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

    AssertionError If the assertion fails.

  • Asserts that a value is not a string.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, string>

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is not a symbol.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, symbol>

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is not exactly undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<Actual, undefined>

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Type Parameters

    • const Actual

    Parameters

    • this: void
    • actual: Actual
    • OptionalfailureMessage: string

    Returns asserts actual is Exclude<
        Actual,
        `${string}-${string}-${string}-${string}-${string}`,
    >

    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.

  • Asserts that a value is exactly null.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is null

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Parameters

    • this: void
    • 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.

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

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is number

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is UnknownObject

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

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

    AssertionError If the assertion failed.

  • Asserts that a number is outside the provided min and max bounds, exclusive.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • __namedParameters: MinMax
    • OptionalfailureMessage: string

    Returns void

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

    assert.isOutBounds(5, {min: 1, max: 10}); // fails
    assert.isOutBounds(10, {min: 1, max: 10}); // fails
    assert.isOutBounds(11, {min: 1, max: 10}); // passes
    assert.isOutBounds(0, {min: 1, max: 10}); // passes

    AssertionError If the assertion fails.

  • Asserts that a value is a JavaScript primitive.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is Primitive

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

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

    AssertionError If the assertion fails.

  • Asserts that a value is a Promise instance.

    Type guards the value.

    Parameters

    • this: void
    • 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.

  • 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.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is PromiseLike<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.isPromiseLike(Promise.resolve(5)); // passes
    assert.isPromiseLike(new CustomThenable(5)); // passes
    assert.isPromiseLike(5); // fails

    AssertionError If the assertion fails.

  • 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.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is PropertyKey

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

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

    AssertionError If the assertion fails.

  • Asserts that a value is a string.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is string

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is a symbol.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is symbol

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

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

    AssertionError If the assertion failed.

  • Asserts that a value is exactly true.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown
    • OptionalfailureMessage: string

    Returns asserts input is true

    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.

  • Asserts that a value is truthy.

    Type guards the value.

    Type Parameters

    • const Actual

    Parameters

    • this: void
    • input: Actual
    • OptionalfailureMessage: string

    Returns asserts input is Exclude<Actual, FalsyValue>

    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.

  • Asserts that a value is exactly undefined.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown
    • OptionalfailureMessage: string

    Returns asserts actual is undefined

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

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

    AssertionError If the assertion failed.

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

    Type guards the value.

    Parameters

    • this: void
    • 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.

  • 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.

    Type Parameters

    • const Actual
    • const Expected

    Parameters

    • this: void
    • actual: Actual
    • expected: Expected
    • OptionalfailureMessage: string

    Returns asserts actual is Expected

    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.

  • Asserts that a parent value does not have the key.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

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

    Returns asserts parent is Exclude<Parent, Record<Key, any>>

    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.

  • Asserts that a parent value none of the keys.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

    • this: void
    • parent: Parent
    • keys: readonly Key[]
    • OptionalfailureMessage: string

    Returns asserts parent is Exclude<Parent, Partial<Record<Key, any>>>

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • parent: string | object
    • value: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • parent: string | object
    • values: unknown[]
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Type guards the first value.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: string
    • expected: RegExp
    • OptionalfailureMessage: string

    Returns void

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

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

    AssertionError If the assertion fails.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: string
    • expected: RegExp
    • OptionalfailureMessage: string

    Returns void

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

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

    AssertionError If the assertion fails.

  • 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.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown
    • OptionalfailureMessage: string

    Returns void

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

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

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

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

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

    AssertionError If both inputs are deeply equal.

  • 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.

    Parameters

    Returns void

    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.

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

    Type guards the value.

    Type Parameters

    • const Actual
    • const Instance

    Parameters

    • this: void
    • instance: Actual
    • constructor: Constructor<Instance>
    • OptionalfailureMessage: string

    Returns asserts instance is Exclude<Actual, Instance>

    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.

  • 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.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Performs no type guarding.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown
    • OptionalfailureMessage: string

    Returns void

    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.

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

    Type guards the first value.

    Type Parameters

    • const Actual
    • const Expected

    Parameters

    • this: void
    • actual: Actual
    • expected: Expected
    • OptionalfailureMessage: string

    Returns asserts actual is Expected

    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.