@cbnsndwch/zero-sources

invariant() function

Home > @cbnsndwch/zero-contracts > invariant

invariant() function

Throws an error of the specified type with the given message if the condition is not met.

Signature:

declare function invariant<TError extends Error = Error>(
    condition: boolean | (() => boolean),
    message: string | (() => string),
    errorClass?: Type<TError>,
    logger?: LoggerService
): asserts condition;

Parameters

Parameter

Type

Description

condition

boolean | (() => boolean)

The condition to check or a function that return the condition.

message

string | (() => string)

The error message to throw if the condition is false.

errorClass

Type<TError>

(Optional) An optional error class to throw if the item is undefined. Defaults to Error

logger

LoggerService

(Optional) An optional logger service to log the error message if the condition is false.

Returns:

asserts condition

How was this page?