Function check

A group of guard methods that return a boolean type guard rather than an assertion type guard.

This can also be called as a standalone check function which returns a boolean to indicate whether its input is truthy or not.

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

const value: unknown = 'some value' as unknown;
if (check.isString(value)) {
// `value` will now be typed as a `string` in here
}

Properties

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

Checks 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 {check} from '@augment-vir/assert';

check.endsWith('ab', 'b'); // returns `true`
check.endsWith('ab', 'a'); // returns `false`
check.endsWith(['a', 'b'], 'b'); // returns `true`
check.endsWith(['a', 'b'], 'a'); // returns `false`
endsWithout: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): boolean;
    (parent: string, child: string, failureMessage?: string): boolean;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): boolean;
} = ...

Checks 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 {check} from '@augment-vir/assert';

check.endsWithout('ab', 'b'); // returns `false`
check.endsWithout('ab', 'a'); // returns `true`
check.endsWithout(['a', 'b'], 'b'); // returns `false`
check.endsWithout(['a', 'b'], 'a'); // returns `true`
hasKey: <const Key extends PropertyKey, const Parent>(
    this: void,
    parent: Parent,
    key: Key,
) => parent is CombineTypeWithKey<Key, Parent>

Checks that a parent value has the key.

Type guards the parent value.

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

check.hasKey({a: 0, b: 1}, 'a'); // returns `true`
check.hasKey({a: 0, b: 1}, 'c'); // returns `false`
isEnumValue: <const Expected extends EnumBaseType>(
    this: void,
    child: unknown,
    checkEnum: Expected,
) => child is Expected[keyof Expected]

Checks that a child value is an enum member.

Type guards the child value.

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

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

check.isEnumValue('a', MyEnum); // returns `true`
check.isEnumValue('A', MyEnum); // returns `false`
isLengthAtLeast: {
    <Element, Length extends number>(
        actual: readonly (undefined | Element)[],
        length: Length,
    ): actual is readonly [Tuple<Element, Length>, Element];
    (actual: string | AnyObject, length: number): boolean;
} = ...

Checks 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 {check} from '@augment-vir/assert';

check.isLengthAtLeast(['a', 'b', 'c'], 2); // returns `true`
check.isLengthAtLeast(['a', 'b', 'c'], 3); // returns `true`
check.isLengthAtLeast(['a', 'b'], 3); // returns `false`
isLengthExactly: {
    <Element, Length extends number>(
        actual: readonly (undefined | Element)[],
        length: Length,
    ): actual is Tuple<Element, Length>;
    (actual: string | AnyObject, length: number): boolean;
} = ...

Checks 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 {check} from '@augment-vir/assert';

check.isLengthExactly(['a', 'b', 'c'], 2); // fails
check.isLengthExactly(['a', 'b', 'c'], 3); // passes
check.isLengthExactly(['a', 'b'], 3); // fails
output: {
    <const FunctionToCall extends AnyFunction>(
        functionToCall: FunctionToCall,
        inputs: Parameters<NoInfer<FunctionToCall>>,
        expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>,
        failureMessage?: string,
    ): OutputReturn<FunctionToCall, boolean>;
    <const FunctionToCall extends AnyFunction>(
        asserter: CustomOutputAsserter<NoInfer<FunctionToCall>>,
        functionToCall: FunctionToCall,
        inputs: Parameters<NoInfer<FunctionToCall>>,
        expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>,
        failureMessage?: string,
    ): OutputReturn<FunctionToCall, boolean>;
} = checkOutput

Checks 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 the default "deeply equals" to whatever you want.

Performs no type guarding.

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

check.output((input: number) => String(input), [5], '5'); // returns `true`
check.output((input: number) => String(input), [10], '5'); // returns `false`

check.output(assert.isLengthAtLeast, (input: number) => String(input), [5], 2); // returns `false`
check.output(assert.isLengthAtLeast, (input: number) => String(input), [10], 2); // returns `true`
startsWith: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): boolean;
    (parent: string, child: string, failureMessage?: string): boolean;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): boolean;
} = ...

Checks 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 {check} from '@augment-vir/assert';

check.startsWith('ab', 'b'); // returns `false`
check.startsWith('ab', 'a'); // returns `true`
check.startsWith(['a', 'b'], 'b'); // returns `false`
check.startsWith(['a', 'b'], 'a'); // returns `true`
startsWithout: {
    <const ArrayElement>(
        parent: readonly ArrayElement[],
        child: ArrayElement,
        failureMessage?: string,
    ): boolean;
    (parent: string, child: string, failureMessage?: string): boolean;
    (
        parent: string | readonly string[],
        child: string,
        failureMessage?: string,
    ): boolean;
} = ...

Checks 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 {check} from '@augment-vir/assert';

check.startsWith('ab', 'b'); // returns `false`
check.startsWith('ab', 'a'); // returns `true`
check.startsWith(['a', 'b'], 'b'); // returns `false`
check.startsWith(['a', 'b'], 'a'); // returns `true`
throws: {
    (
        this: void,
        callbackOrPromise: () => never,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
    ): boolean;
    (
        this: void,
        callbackOrPromise: Promise<any> | () => Promise<any>,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
    ): Promise<boolean>;
    (
        this: void,
        callback: () => any,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
    ): boolean;
    (
        this: void,
        callback: Promise<any> | () => any,
        matchOptions?: PartialWithNullable<
            {
                matchConstructor: ErrorConstructor
                | new (...args: any[]) => Error;
                matchMessage: string | RegExp;
            },
        >,
    ): MaybePromise<boolean>;
} = throwsCheck

If a function input is provided:

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

If a promise is provided:

Awaits the promise and checks 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 {check} from '@augment-vir/assert';

check.throws(() => {
throw new Error();
}); // returns `true`
check.throws(
() => {
throw new Error();
},
{matchMessage: 'hi'},
); // returns `false`
await check.throws(Promise.reject()); // returns `true`
check.throws(() => {}); // returns `false`

Methods

  • Checks that a parent value has all the keys.

    Type guards the parent value.

    Type Parameters

    • const Keys extends PropertyKey
    • const Parent

    Parameters

    Returns parent is CombineTypeWithKey<Keys, Parent>

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

    check.hasKeys({a: 0, b: 1}, ['a', 'b']); // returns `true`
    check.hasKeys({a: 0, b: 1}, ['b', 'c']); // returns `false`
  • Checks that an object/array parent includes a child value through reference equality.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    const child = {a: 'a'};

    check.hasValue({child}, child); // returns `true`
    check.hasValue({child: {a: 'a'}}, child); // returns `false`
    check.hasValue([child], child); // returns `true`
  • Checks that an object/array parent includes all child values through reference equality.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

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

    check.hasValues({child, child2}, [child, child2]); // returns `true`
    check.hasValues({child: {a: 'a'}, child2}, [child, child2]); // returns `false`
    check.hasValues([child], [child, child2]); // returns `true`
  • Checks 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>

    Returns instance is Instance

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

    check.instanceOf(/abc/, RegExp); // returns `true`
    check.instanceOf('abc', RegExp); // returns `false`
  • Checks that a number is above the expectation (actual > expected).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number

    Returns boolean

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

    check.isAbove(10, 5); // returns `true`
    check.isAbove(5, 5); // returns `false`
    check.isAbove(5, 10); // returns `false`
  • Checks that a number is within ±delta of the expectation.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    check.isApproximately(10, 8, 4); // returns `true`
    check.isApproximately(10, 12, 4); // returns `true`
    check.isApproximately(10, 8, 1); // returns `false`
    check.isApproximately(10, 12, 1); // returns `false`
  • Checks that a number is at least the expectation (actual >= expected).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number

    Returns boolean

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

    check.isAtLeast(10, 5); // returns `true`
    check.isAtLeast(5, 5); // returns `true`
    check.isAtLeast(5, 10); // returns `false`
  • Checks that a number is at most the expectation (actual <= expected).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number

    Returns boolean

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

    check.isAtMost(10, 5); // returns `false`
    check.isAtMost(5, 5); // returns `true`
    check.isAtMost(5, 10); // returns `true`
  • Checks that a number is below the expectation (actual < expected).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number
    • expected: number

    Returns boolean

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

    check.isBelow(10, 5); // returns `false`
    check.isBelow(5, 5); // returns `false`
    check.isBelow(5, 10); // returns `true`
  • Checks that a value is defined (not null and not undefined).

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns input is Exclude<Actual, undefined | null>

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

    check.isDefined(0); // returns `true`
    check.isDefined(''); // returns `true`
    check.isDefined(null); // returns `false`
    check.isDefined(undefined); // returns `false`
  • Checks 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.

    Parameters

    • this: void
    • actual: unknown
    • OptionalmatchOptions: PartialWithNullable<
          {
              matchConstructor: ErrorConstructor
              | new (...args: any[]) => Error;
              matchMessage: string | RegExp;
          },
      >

    Returns actual is Error

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

    check.isError(new Error()); // returns `true`
    check.isError(new Error(), {matchMessage: 'hi'}); // returns `false`
    check.isError({message: 'not an error'}); // returns `false`
  • Checks that a value is exactly false.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown

    Returns input is false

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

    check.isFalse(true); // returns `false`
    check.isFalse(false); // returns `true`
    check.isFalse(1); // returns `false`
    check.isFalse(0); // returns `false`
  • Checks that a value is falsy.

    Type guards the value when possible.

    Parameters

    • this: void
    • input: unknown

    Returns input is FalsyValue

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

    check.isFalsy(true); // returns `false`
    check.isFalsy(false); // returns `true`
    check.isFalsy(1); // returns `false`
    check.isFalsy(0); // returns `true`
  • Checks that a number is finite: meaning, not NaN and not Infinity or -Infinity.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number

    Returns boolean

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

    check.isFinite(10); // returns `true`
    check.isFinite(parseInt('invalid')); // returns `false`
    check.isFinite(Infinity); // returns `false`
    check.isFinite(-Infinity); // returns `false`
  • Checks 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

    Returns child is Values<Parent>

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

    const child = {a: 'a'};

    check.isIn(child, {child}); // returns `true`
    check.isIn('a', 'ab'); // returns `true`
    check.isIn(child, [child]); // returns `true`

    check.isIn(child, {child: {a: 'a'}}); // returns `false`
    check.isIn('a', 'bc'); // returns `false`
  • Checks that a number is inside the provided min and max bounds, inclusive.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    check.isInBounds(5, {min: 1, max: 10}); // passes
    check.isInBounds(10, {min: 1, max: 10}); // passes
    check.isInBounds(11, {min: 1, max: 10}); // fails
    check.isInBounds(0, {min: 1, max: 10}); // fails
  • Checks that a number is either Infinity or -Infinity.

    Performs no type guarding.

    Parameters

    • this: void
    • actual: number

    Returns boolean

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

    check.isInfinite(10); // returns `false`
    check.isInfinite(parseInt('invalid')); // returns `false`
    check.isInfinite(Infinity); // returns `true`
    check.isInfinite(-Infinity); // returns `true`
  • Checks that a key is contained within a parent value.

    Type guards the key.

    Type Parameters

    • const Parent

    Parameters

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

    Returns key is keyof Parent

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

    check.isKeyof('a', {a: 0, b: 1}); // returns `true`
    check.isKeyof('c', {a: 0, b: 1}); // returns `false`
  • Checks that a number is NaN.

    Performs no type guarding.

    Parameters

    • this: void
    • input: number

    Returns boolean

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

    check.isNaN(10); // returns `false`
    check.isNaN(parseInt('invalid')); // returns `true`
    check.isNaN(Infinity); // returns `false`
  • Checks that a number is outside ±delta of the expectation.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    check.isNotApproximately(10, 8, 4); // returns `false`
    check.isNotApproximately(10, 12, 4); // returns `false`
    check.isNotApproximately(10, 8, 1); // returns `true`
    check.isNotApproximately(10, 12, 1); // returns `true`
  • Checks that a value is not an array.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

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

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

    check.isNotArray([]); // returns `false`
    check.isNotArray({length: 4}); // returns `true`
  • Checks that a value is not a BigInt.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, bigint>

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

    check.isNotBigInt(123n); // returns `false`
    check.isNotBigInt(123); // returns `true`
  • Checks that a value is not a boolean.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, boolean>

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

    check.isNotBoolean(true); // returns `false`
    check.isNotBoolean('true'); // returns `true`
  • Checks that a value is not empty. Supports strings, Maps, Sets, objects, and arrays.

    Type guards the value.

    Type Parameters

    Parameters

    Returns actual is Exclude<Actual, Empty>

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

    check.isNotEmpty({}); // returns `false`
    check.isNotEmpty(''); // returns `false`
    check.isNotEmpty([]); // returns `false`

    check.isNotEmpty('a'); // returns `true`
    check.isNotEmpty({a: 'a'}); // returns `true`
  • Checks 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

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

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

    const child = {a: 'a'};

    check.isNotIn(child, {child}); // returns `false`
    check.isNotIn('a', 'ab'); // returns `false`
    check.isNotIn(child, [child]); // returns `false`

    check.isNotIn(child, {child: {a: 'a'}}); // returns `true`
    check.isNotIn('a', 'bc'); // returns `true`
  • Checks that a key is not contained within a parent value.

    Type guards the key.

    Type Parameters

    • const Key extends PropertyKey
    • const Parent

    Parameters

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

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

    check.isNotKeyOf('a', {a: 0, b: 1}); // returns `false`
    check.isNotKeyOf('c', {a: 0, b: 1}); // returns `true`
  • Checks that a value is not exactly null.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, null>

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

    check.isNotNull(null); // returns `false`
    check.isNotNull(undefined); // returns `true`
  • Checks that a value is not a number. This includes NaN.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, number>

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

    check.isNotNumber(123); // returns `false`
    check.isNotNumber(123n); // returns `true`
  • Checks 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 Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, Primitive>

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

    check.isNotPrimitive('key'); // returns `false`
    check.isNotPrimitive(true); // returns `false`
    check.isNotPrimitive({}); // returns `true`
  • Checks that a value is a not Promise instance.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, Promise<any>>

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

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

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

    check.isNotPromise(Promise.resolve(5)); // returns `false`
    check.isNotPromise(new CustomThenable(5)); // returns `true`
    check.isNotPromise(5); // returns `true`
  • Checks 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

    Returns actual is Exclude<Actual, PromiseLike<any>>

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

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

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

    check.isNotPromiseLike(Promise.resolve(5)); // returns `false`
    check.isNotPromiseLike(new CustomThenable(5)); // returns `false`
    check.isNotPromiseLike(5); // returns `true`
  • Checks 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

    Returns actual is Exclude<Actual, PropertyKey>

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

    check.isNotPropertyKey('key'); // returns `false`
    check.isNotPropertyKey(true); // returns `true`
    check.isNotPropertyKey({}); // returns `true`
  • Checks that a value is not a string.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, string>

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

    check.isNotString(''); // returns `false`
    check.isNotString(5); // returns `true`
  • Checks that a value is not a symbol.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, symbol>

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

    check.isNotSymbol(Symbol('my-symbol')); // returns `false`
    check.isNotSymbol('my-symbol'); // returns `true`
  • Checks that a value is not exactly undefined.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Exclude<Actual, undefined>

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

    check.isNotUndefined(undefined); // returns `false`
    check.isNotUndefined(null); // returns `true`
  • Checks that a value is not a valid UUID. The nil or max UUIDs are included as not valid.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

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

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

    check.isNotUuid(createUuidV4()); // returns `false`
    check.isNotUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // returns `false`
    check.isNotUuid('00000000-0000-0000-0000-000000000000'); // returns `true`
    check.isNotUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // returns `true`
    check.isNotUuid('not-a-uuid'); // returns `true`
  • Checks that a value is nullish (null or undefined).

    Type guards the value.

    Parameters

    • this: void
    • input: unknown

      The value to check.

    Returns input is undefined | null

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

    check.isNullish(0); // returns `false`
    check.isNullish(''); // returns `false`
    check.isNullish(null); // returns `true`
    check.isNullish(undefined); // returns `true`
  • Checks that a number is outside the provided min and max bounds, exclusive.

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    check.isOutBounds(5, {min: 1, max: 10}); // fails
    check.isOutBounds(10, {min: 1, max: 10}); // fails
    check.isOutBounds(11, {min: 1, max: 10}); // passes
    check.isOutBounds(0, {min: 1, max: 10}); // passes
  • Checks that a value is a JavaScript primitive.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown

    Returns actual is Primitive

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

    check.isPrimitive('key'); // returns `true`
    check.isPrimitive(true); // returns `true`
    check.isPrimitive({}); // returns `false`
  • Checks that a value is a Promise instance.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Extract<Actual, Promise<any>>

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

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

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

    check.isPromise(Promise.resolve(5)); // returns `true`
    check.isPromise(new CustomThenable(5)); // returns `false`
    check.isPromise(5); // returns `false`
  • Checks 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.

    Type Parameters

    • Actual

    Parameters

    Returns actual is Extract<Actual, PromiseLike<any>>

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

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

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

    check.isPromiseLike(Promise.resolve(5)); // returns `true`
    check.isPromiseLike(new CustomThenable(5)); // returns `true`
    check.isPromiseLike(5); // returns `false`
  • Checks that a value is not a JavaScript primitive.

    Type guards the value.

    Type Parameters

    • Actual

    Parameters

    Returns actual is
        | Extract<string, Actual>
        | Extract<number, Actual>
        | Extract<symbol, Actual>

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

    check.isPropertyKey('key'); // returns `true`
    check.isPropertyKey(true); // returns `false`
    check.isPropertyKey({}); // returns `false`
  • Checks that a value is exactly true.

    Type guards the value.

    Parameters

    • this: void
    • input: unknown

    Returns input is true

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

    check.isTrue(true); // returns `true`
    check.isTrue(false); // returns `false`
    check.isTrue(1); // returns `false`
    check.isTrue(0); // returns `false`
  • Checks that a value is truthy.

    Type guards the value.

    Type Parameters

    • T

    Parameters

    • this: void
    • input: T

    Returns input is Exclude<T, FalsyValue>

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

    check.isTruthy(true); // passes
    check.isTruthy(false); // fails
    check.isTruthy(1); // passes
    check.isTruthy(0); // fails
  • Checks that a value is a valid UUID. Does not accept the nil or max UUIDs.

    Type guards the value.

    Parameters

    • this: void
    • actual: unknown

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

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

    check.isUuid(createUuidV4()); // returns `true`
    check.isUuid('29e0f18e-6115-4982-8342-0afcadf5d611'); // returns `true`
    check.isUuid('00000000-0000-0000-0000-000000000000'); // returns `false`
    check.isUuid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'); // returns `false`
    check.isUuid('not-a-uuid'); // returns `false`
  • Checks 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

    • Actual
    • Expected

    Parameters

    Returns actual is Expected

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

    check.jsonEquals({a: 'a'}, {a: 'a'}); // true

    check.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // false
  • Checks that a parent value does not have the key.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

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

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

    check.lacksKey({a: 0, b: 1}, 'a'); // returns `false`
    check.lacksKey({a: 0, b: 1}, 'c'); // returns `true`
  • Checks that a parent value none of the keys.

    Type guards the parent value.

    Type Parameters

    • const Parent
    • const Key extends PropertyKey

    Parameters

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

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

    check.lacksKeys({a: 0, b: 1}, ['b', 'c']); // returns `false`
    check.lacksKeys({a: 0, b: 1}, ['c', 'd']); // returns `true`
  • Checks 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

    Returns boolean

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

    const child = {a: 'a'};

    check.lacksValue({child}, child); // returns `false`
    check.lacksValue({child: {a: 'a'}}, child); // returns `true`
    check.lacksValue([child], child); // returns `false`
  • Checks 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[]

    Returns boolean

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

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

    check.lacksValues({}, [child, child2]); // returns `true`
    check.lacksValues({child, child2}, [child, child2]); // returns `false`
    check.lacksValues({child: {a: 'a'}, child2}, [child, child2]); // returns `false`
  • Checks that two values are loosely equal (using ==).

    Type guards the first value.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown

    Returns boolean

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

    check.looseEquals('a', 'a'); // true

    check.looseEquals('1', 1); // true

    check.looseEquals({a: 'a'}, {a: 'a'}); // false

    const objectExample = {a: 'a'};
    check.looseEquals(objectExample, objectExample); // true
  • Checks that a string (first input, actual) matches a RegExp (second input, expected).

    Performs no type guarding.

    Parameters

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

    Returns boolean

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

    check.matches('hi', /^h/); // returns `true`
    check.matches('hi', /^g/); // returns `false`
  • Checks 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

    Returns boolean

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

    check.mismatches('hi', /^h/); // returns `false`
    check.mismatches('hi', /^g/); // returns `true`
  • Checks 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

    Returns boolean

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

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

    check.deepEquals('1', 1); // true

    check.deepEquals({a: 'a'}, {a: 'a'}); // false

    const objectExample = {a: 'a'};
    check.deepEquals(objectExample, objectExample); // false
  • Checks 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 boolean

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

    check.notEntriesEqual({a: 'a'}, {a: 'a'}); // false

    check.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // true

    const bExample = {b: 'b'};
    check.notEntriesEqual({a: bExample}, {a: bExample}); // false
  • Checks that a value is not an instance of the given class constructor.

    Type guards the value.

    Type Parameters

    • const Actual
    • const Instance

    Parameters

    Returns instance is Exclude<Actual, Instance>

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

    check.notInstanceOf(/abc/, RegExp); // returns `false`
    check.notInstanceOf('abc', RegExp); // returns `true`
  • Checks 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

    Returns boolean

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

    check.notJsonEquals({a: 'a'}, {a: 'a'}); // false

    check.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // true
  • Checks that two values are not loosely equal (using ==).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown

    Returns boolean

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

    check.notLooseEquals('a', 'a'); // false

    check.notLooseEquals('1', 1); // false

    check.notLooseEquals({a: 'a'}, {a: 'a'}); // true

    const objectExample = {a: 'a'};
    check.notLooseEquals(objectExample, objectExample); // false
  • Checks that two values are not strictly equal (using ===).

    Performs no type guarding.

    Parameters

    • this: void
    • actual: unknown
    • expected: unknown

    Returns boolean

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

    check.notStrictEquals('a', 'a'); // false

    check.notStrictEquals('1', 1); // true

    check.notStrictEquals({a: 'a'}, {a: 'a'}); // true

    const objectExample = {a: 'a'};
    check.notStrictEquals(objectExample, objectExample); // false