invariant es-toolkit
Asserts that a given condition is true. If the condition is false, it throws an error.
Usage
invariant(condition, message)
Use invariant when a specific condition must be satisfied in your code. If the condition is false, it immediately throws an error to stop program execution.
You can also pass an error object directly.
It is particularly useful for validating code assumptions during development or ensuring that function inputs are within expected ranges.
Parameters
The condition to evaluate. If it evaluates to a falsy value, an error is thrown.
The error message or error object to throw when the condition is false.
Returns
Returns nothing if the condition is true.
Errors
Throws the provided message or error object if the condition evaluates to false.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.