Skip to main content

Assert

Helper functions that have to do with asserting.

Functions

assertArray()

function assertArray(value, msg): asserts value is unknown[]

Defined in: functions/assert.ts:12

Helper function to throw an error if the provided value is not an array.

Parameters

ParameterType
valueunknown
msgstring

Returns

asserts value is unknown[]


assertBoolean()

function assertBoolean(value, msg): asserts value is boolean

Defined in: functions/assert.ts:22

Helper function to throw an error if the provided value is not a boolean.

Parameters

ParameterType
valueunknown
msgstring

Returns

asserts value is boolean


assertDefined()

function assertDefined<T>(value, ...__namedParameters): asserts value is Exclude<T, undefined>

Defined in: functions/assert.ts:36

Helper function to throw an error if the provided value is equal to undefined.

This is useful to have TypeScript narrow a T | undefined value to T in a concise way.

Type Parameters

Type Parameter
T

Parameters

ParameterType
valueT
...__namedParameters[undefined] extends [T] ? [string] : ["The assertion is useless because the provided value does not contain undefined."]

Returns

asserts value is Exclude<T, undefined>


assertEnumValue()

function assertEnumValue<T>(
value,
transpiledEnum,
msg,
set?): asserts value is T[keyof T]

Defined in: functions/assert.ts:59

Helper function to throw an error if the provided value is not contained within an enum.

Type Parameters

Type Parameter
T extends TranspiledEnum

Parameters

ParameterTypeDescription
valuestring | numberThe value to check.
transpiledEnumTThe enum to check against.
msgstringThe error message to throw if the check fails.
set?ReadonlySet<string | number>Optional. A set that contains all of the values of an enum. If provided, this function will check for existence using the set (instead of the enum itself). Using a set should be more performant for enums with around 52 or more elements.

Returns

asserts value is T[keyof T]


assertNotNull()

function assertNotNull<T>(value, ...__namedParameters): asserts value is Exclude<T, null>

Defined in: functions/assert.ts:75

Helper function to throw an error if the provided value is equal to null.

This is useful to have TypeScript narrow a T | null value to T in a concise way.

Type Parameters

Type Parameter
T

Parameters

ParameterType
valueT
...__namedParameters[null] extends [T] ? [string] : ["The assertion is useless because the provided value does not contain null."]

Returns

asserts value is Exclude<T, null>


assertNumber()

function assertNumber(value, msg): asserts value is number

Defined in: functions/assert.ts:89

Helper function to throw an error if the provided value is not a number.

Parameters

ParameterType
valueunknown
msgstring

Returns

asserts value is number


assertObject()

function assertObject(value, msg): asserts value is Record<string, unknown>

Defined in: functions/assert.ts:107

Helper function to throw an error if the provided value is not an object (i.e. a TypeScript record).

This is useful to have TypeScript narrow a Record<string, unknown> | undefined value to Record<string, unknown> in a concise way.

Under the hood, this function uses the isObject helper function.

Parameters

ParameterType
valueunknown
msgstring

Returns

asserts value is Record<string, unknown>


assertString()

function assertString(value, msg): asserts value is string

Defined in: functions/assert.ts:117

Helper function to throw an error if the provided value is not a string.

Parameters

ParameterType
valueunknown
msgstring

Returns

asserts value is string