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
}

@augment-vir/assert

Properties

deepEquals: (<Actual, Expected>(actual: Actual, expected: Expected) => actual is Expected) = ...

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

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

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

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

const objectExample = {a: 'a'};
check.deepEquals(objectExample, objectExample); // true
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`
entriesEqual: (<Actual, Expected>(actual: Actual, expected: Expected) => actual is Expected) = ...

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

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

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

const bExample = {b: 'b'};
check.entriesEqual({a: bExample}, {a: bExample}); // true
hasKey: (<const Parent, const Key>(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`
hasKeys: (<const Keys, const Parent>(parent: Parent, keys: readonly Keys[]) => parent is CombineTypeWithKey<Keys, Parent>) = ...

Checks that a parent value has all the keys.

Type guards the parent value.

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`
hasValue: ((input: object, ...params: [value: unknown]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
hasValues: ((input: object, ...params: [values: readonly unknown[]]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
instanceOf: (<const Instance>(instance: unknown, constructor: Constructor<Instance>) => instance is Instance) = ...

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

Type guards the value.

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

check.instanceOf(/abc/, RegExp); // returns `true`
check.instanceOf('abc', RegExp); // returns `false`
isAbove: ((input: number, ...params: [expected: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.isAbove(10, 5); // returns `true`
check.isAbove(5, 5); // returns `false`
check.isAbove(5, 10); // returns `false`
isApproximately: ((input: number, ...params: [expected: number, delta: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
isArray: (<Actual>(actual: Actual) => actual is ArrayNarrow<Actual>) = ...

Checks that a value is an array.

Type guards the value.

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

check.isArray([]); // returns `true`
check.isArray({length: 4}); // returns `false`
isAtLeast: ((input: number, ...params: [expected: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.isAtLeast(10, 5); // returns `true`
check.isAtLeast(5, 5); // returns `true`
check.isAtLeast(5, 10); // returns `false`
isAtMost: ((input: number, ...params: [expected: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.isAtMost(10, 5); // returns `false`
check.isAtMost(5, 5); // returns `true`
check.isAtMost(5, 10); // returns `true`
isBelow: ((input: number, ...params: [expected: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.isBelow(10, 5); // returns `false`
check.isBelow(5, 5); // returns `false`
check.isBelow(5, 10); // returns `true`
isBigInt: ((input: unknown, ...params: []) => input is bigint) = autoGuardSymbol

Checks that a value is a BigInt.

Type guards the value.

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

check.isBigInt(123n); // returns `true`
check.isBigInt(123); // returns `false`
isBoolean: ((input: unknown, ...params: []) => input is boolean) = autoGuardSymbol

Checks that a value is a boolean.

Type guards the value.

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

check.isBoolean(true); // returns `true`
check.isBoolean('true'); // returns `false`
isDefined: (<Actual>(input: Actual) => input is Exclude<Actual, undefined | null>) = ...

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

Type guards the value.

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`
isEmpty: (<const Actual>(actual: Actual) => actual is NarrowToActual<Actual, Empty>) = ...

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

Type guards the value.

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

check.isEmpty({}); // returns `true`
check.isEmpty(''); // returns `true`
check.isEmpty([]); // returns `true`

check.isEmpty('a'); // returns `false`
check.isEmpty({a: 'a'}); // returns `false`
isEnumValue: (<const Expected>(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`
isError: ((input: unknown, ...params: [matchOptions?: PartialWithNullable<{
    matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
    matchMessage: string | RegExp;
}>]) => input is Error) = autoGuardSymbol

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.

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`
isFalse: ((input: unknown, ...params: []) => input is false) = autoGuardSymbol

Checks that a value is exactly false.

Type guards the value.

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`
isFalsy: ((input: unknown, ...params: []) => input is FalsyValue) = autoGuardSymbol

Checks that a value is falsy.

Type guards the value when possible.

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`
isFinite: ((input: number, ...params: []) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
isFunction: (<const Actual>(actual: Actual) => actual is NarrowToActual<Actual, AnyFunction>) = ...

Checks that a value is a function.

Type guards the value.

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

check.isFunction(() => {}); // returns `true`
check.isFunction({}); // returns `false`
isHttpStatus: ((input: unknown, ...params: []) => input is HttpStatus) = autoGuardSymbol

Checks that a value is an HttpStatus.

Type guards the value.

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

check.isHttpStatus(400); // returns `true`
check.isHttpStatus(500); // returns `true`
check.isHttpStatus(99); // returns `false`
isHttpStatusCategory: (<const Actual, const Category>(actual: Actual, category: Category, failureMessage?: string) => actual is NarrowToExpected<Actual, HttpStatusByCategory<Category>>) = ...

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

Type guards the value.

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

check.isHttpStatusCategory(400, HttpStatusCategory.ClientError); // returns `true`
check.isHttpStatusCategory(500, HttpStatusCategory.Success); // returns `false`
check.isHttpStatusCategory(99, HttpStatusCategory.Information); // returns `false`
isIn: (<const Parent>(child: unknown, parent: Parent) => child is Values<Parent>) = ...

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

Type guards the child when possible.

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`
isInfinite: ((input: number, ...params: []) => input is unknown) = autoGuardSymbol

Checks that a number is either Infinity or -Infinity.

Performs no type guarding.

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`
isKeyOf: (<const Parent>(key: PropertyKey, parent: Parent) => key is keyof Parent) = ...

Checks that a key is contained within a parent value.

Type guards the key.

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

check.isKeyof('a', {a: 0, b: 1}); // returns `true`
check.isKeyof('c', {a: 0, b: 1}); // returns `false`
isLengthAtLeast: {
    <Element, Length>(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.

Type declaration

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>(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
isNaN: ((input: number, ...params: []) => input is unknown) = autoGuardSymbol

Checks that a number is NaN.

Performs no type guarding.

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

check.isNaN(10); // returns `false`
check.isNaN(parseInt('invalid')); // returns `true`
check.isNaN(Infinity); // returns `false`
isNotApproximately: ((input: number, ...params: [expected: number, delta: number]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
isNotArray: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, readonly unknown[]>) = ...

Checks that a value is not an array.

Type guards the value.

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

check.isNotArray([]); // returns `false`
check.isNotArray({length: 4}); // returns `true`
isNotBigInt: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, bigint>) = ...

Checks that a value is not a BigInt.

Type guards the value.

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

check.isNotBigInt(123n); // returns `false`
check.isNotBigInt(123); // returns `true`
isNotBoolean: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, boolean>) = ...

Checks that a value is not a boolean.

Type guards the value.

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

check.isNotBoolean(true); // returns `false`
check.isNotBoolean('true'); // returns `true`
isNotEmpty: (<const Actual>(actual: Actual) => actual is Exclude<Actual, Empty>) = ...

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

Type guards the value.

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`
isNotEnumValue: (<const Actual, const Expected>(child: Actual, checkEnum: Expected) => child is Exclude<Actual, Expected[keyof Expected] | `${Expected[keyof Expected]}`>) = ...

Checks that a child value is not an enum member.

Type guards the child value.

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

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

check.isNotEnumValue('a', MyEnum); // returns `false`
check.isNotEnumValue('A', MyEnum); // returns `true`
isNotFunction: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, AnyFunction>) = ...

Checks that a value is not a function.

Type guards the value.

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

check.isNotFunction(() => {}); // returns `false`
check.isNotFunction({}); // returns `true`
isNotIn: (<const Parent, const Child>(child: Child, parent: Parent, failureMessage?: string) => child is Exclude<Child, Values<Parent>>) = ...

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

Type guards the child when possible.

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`
isNotKeyOf: (<const Key, const Parent>(key: Key, parent: Parent, failureMessage?: string) => key is Exclude<Key, RequiredKeysOf<Parent>>) = ...

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

Type guards the key.

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

check.isNotKeyOf('a', {a: 0, b: 1}); // returns `false`
check.isNotKeyOf('c', {a: 0, b: 1}); // returns `true`
isNotNull: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, null>) = ...

Checks that a value is not exactly null.

Type guards the value.

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

check.isNotNull(null); // returns `false`
check.isNotNull(undefined); // returns `true`
isNotNumber: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, number>) = ...

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

Type guards the value.

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

check.isNotNumber(123); // returns `false`
check.isNotNumber(123n); // returns `true`
isNotObject: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, UnknownObject>) = ...

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

Type guards the value.

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

check.isNotObject({}); // returns `false`
check.isNotObject([]); // returns `true`
isNotPrimitive: (<const Actual>(input: Actual) => input is Exclude<Actual, Primitive>) = ...

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.

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

check.isPropertyKey('key'); // returns `true`
check.isPropertyKey(true); // returns `false`
check.isPropertyKey({}); // returns `false`
isNotPromise: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, Promise<any>>) = ...

Checks that a value is a not Promise instance.

Type guards the value.

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`
isNotPromiseLike: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, PromiseLike<any>>) = ...

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.

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`
isNotPropertyKey: (<const Actual>(input: Actual) => input is Exclude<Actual, PropertyKey>) = ...

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.

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

check.isNotPropertyKey('key'); // returns `false`
check.isNotPropertyKey(true); // returns `true`
check.isNotPropertyKey({}); // returns `true`
isNotString: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, string>) = ...

Checks that a value is not a string.

Type guards the value.

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

check.isNotString(''); // returns `false`
check.isNotString(5); // returns `true`
isNotSymbol: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, symbol>) = ...

Checks that a value is not a symbol.

Type guards the value.

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

check.isNotSymbol(Symbol('my-symbol')); // returns `false`
check.isNotSymbol('my-symbol'); // returns `true`
isNotUndefined: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, undefined>) = ...

Checks that a value is not exactly undefined.

Type guards the value.

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

check.isNotUndefined(undefined); // returns `false`
check.isNotUndefined(null); // returns `true`
isNotUuid: (<const Actual>(actual: Actual, failureMessage?: string) => actual is Exclude<Actual, `${string}-${string}-${string}-${string}-${string}`>) = ...

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

Type guards the value.

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`
isNull: ((input: unknown, ...params: []) => input is null) = autoGuardSymbol

Checks that a value is exactly null.

Type guards the value.

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

check.isNull(null); // returns `true`
check.isNull(undefined); // returns `false`
isNullish: ((input: unknown, ...params: []) => input is undefined | null) = autoGuardSymbol

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

Type guards the value.

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`
isNumber: ((input: unknown, ...params: []) => input is number) = autoGuardSymbol

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

Type guards the value.

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

check.isNumber(123); // returns `true`
check.isNumber(123n); // returns `false`
isObject: ((input: unknown, ...params: []) => input is UnknownObject) = autoGuardSymbol

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

Type guards the value.

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

check.isObject({}); // returns `true`
check.isObject([]); // returns `false`
isPrimitive: ((input: unknown, ...params: []) => input is Primitive) = autoGuardSymbol

Checks that a value is a JavaScript primitive.

Type guards the value.

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

check.isPrimitive('key'); // returns `true`
check.isPrimitive(true); // returns `true`
check.isPrimitive({}); // returns `false`
isPromise: ((input: unknown, ...params: []) => input is Promise<any>) = autoGuardSymbol

Checks that a value is a Promise instance.

Type guards the value.

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`
isPromiseLike: ((input: unknown, ...params: []) => input is PromiseLike<any>) = autoGuardSymbol

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.

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`
isPropertyKey: ((input: unknown, ...params: []) => input is PropertyKey) = autoGuardSymbol

Checks that a value is not a JavaScript primitive.

Type guards the value.

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

check.isPrimitive('key'); // returns `false`
check.isPrimitive(true); // returns `false`
check.isPrimitive({}); // returns `true`
isString: ((input: unknown, ...params: []) => input is string) = autoGuardSymbol

Checks that a value is a string.

Type guards the value.

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

check.isString(''); // returns `true`
check.isString(5); // returns `false`
isSymbol: ((input: unknown, ...params: []) => input is symbol) = autoGuardSymbol

Checks that a value is a symbol.

Type guards the value.

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

check.isSymbol(Symbol('my-symbol')); // returns `true`
check.isSymbol('my-symbol'); // returns `false`
isTrue: ((input: unknown, ...params: []) => input is true) = autoGuardSymbol

Checks that a value is exactly true.

Type guards the value.

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`
isTruthy: (<T>(input: T) => input is Exclude<T, FalsyValue>) = ...

Checks that a value is truthy.

Type guards the value.

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

check.isTruthy(true); // passes
check.isTruthy(false); // fails
check.isTruthy(1); // passes
check.isTruthy(0); // fails
isUndefined: ((input: unknown, ...params: []) => input is undefined) = autoGuardSymbol

Checks that a value is exactly undefined.

Type guards the value.

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

check.isUndefined(undefined); // returns `true`
check.isUndefined(null); // returns `false`
isUuid: ((input: unknown, ...params: []) => input is `${string}-${string}-${string}-${string}-${string}`) = autoGuardSymbol

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

Type guards the value.

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`
jsonEquals: (<Actual, Expected>(actual: Actual, expected: Expected) => actual is Expected) = ...

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.

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

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

check.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // false
lacksKey: (<const Parent, const Key>(parent: Parent, key: Key, failureMessage?: string) => parent is Exclude<Parent, Record<Key, any>>) = ...

Checks that a parent value does not have the key.

Type guards the parent value.

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

check.lacksKey({a: 0, b: 1}, 'a'); // returns `false`
check.lacksKey({a: 0, b: 1}, 'c'); // returns `true`
lacksKeys: (<const Parent, const Key>(parent: Parent, key: readonly Key[]) => parent is Exclude<Parent, Partial<Record<Key, any>>>) = ...

Checks that a parent value none of the keys.

Type guards the parent value.

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`
lacksValue: ((input: object, ...params: [value: unknown]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
lacksValues: ((input: object, ...params: [values: readonly unknown[]]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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`
looseEquals: ((input: unknown, ...params: [expected: unknown]) => input is unknown) = autoGuardSymbol

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

Type guards the first value.

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
matches: ((input: string, ...params: [expected: RegExp]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.matches('hi', /^h/); // returns `true`
check.matches('hi', /^g/); // returns `false`
mismatches: ((input: string, ...params: [expected: RegExp]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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

check.mismatches('hi', /^h/); // returns `false`
check.mismatches('hi', /^g/); // returns `true`
notDeepEquals: ((input: unknown, ...params: [expected: unknown]) => input is unknown) = autoGuardSymbol

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.

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
notEntriesEqual: ((input: object, ...params: [expected: object]) => input is unknown) = autoGuardSymbol

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.

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
notInstanceOf: (<const Actual, const Instance>(instance: Actual, constructor: Constructor<Instance>) => instance is Exclude<Actual, Instance>) = ...

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

Type guards the value.

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

check.notInstanceOf(/abc/, RegExp); // returns `false`
check.notInstanceOf('abc', RegExp); // returns `true`
notJsonEquals: ((input: unknown, ...params: [expected: unknown]) => input is unknown) = autoGuardSymbol

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.

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

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

check.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // true
notLooseEquals: ((input: unknown, ...params: [expected: unknown]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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
notStrictEquals: ((input: unknown, ...params: [expected: unknown]) => input is unknown) = autoGuardSymbol

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

Performs no type guarding.

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
output: {
    <const FunctionToCall>(functionToCall: FunctionToCall, inputs: Parameters<NoInfer<FunctionToCall>>, expectedOutput: Awaited<ReturnType<NoInfer<FunctionToCall>>>, failureMessage?: string): OutputReturn<FunctionToCall, boolean>;
    <const FunctionToCall>(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`
strictEquals: (<Actual, Expected>(actual: Actual, expected: Expected) => actual is Expected) = ...

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

Type guards the first value.

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

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

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

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

const objectExample = {a: 'a'};
check.strictEquals(objectExample, objectExample); // true
throws: {
    (callbackOrPromise: (() => never), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>): boolean;
    (callbackOrPromise: Promise<any> | (() => Promise<any>), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>): Promise<boolean>;
    (callback: (() => any), matchOptions?: PartialWithNullable<{
        matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
        matchMessage: string | RegExp;
    }>): boolean;
    (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`