Skip to content

invariant

Import from @varavel/vdl-plugin-sdk/utils/misc.

Call Signature

function invariant(condition, message): asserts condition;

Asserts that a given condition is true. If the condition is false, an error is thrown with the provided message.

Parameters

Parameter Type Description
condition unknown The condition to evaluate.
message string The error message to throw if the condition is false.

Returns

asserts condition

Returns void if the condition is true.

Throws

Throws an error if the condition is false.

Example

// This call will succeed without any errors
invariant(true, 'This should not throw');

// This call will fail and throw an error with the message 'This should throw'
invariant(false, 'This should throw');

Call Signature

function invariant(condition, error): asserts condition;

Asserts that a given condition is true. If the condition is false, an error is thrown with the provided error.

Parameters

Parameter Type Description
condition unknown The condition to evaluate.
error Error The error to throw if the condition is false.

Returns

asserts condition

Returns void if the condition is true.

Throws

Throws an error if the condition is false.

Example

// This call will succeed without any errors
invariant(true, new Error('This should not throw'));

class CustomError extends Error {
  constructor(message: string) {
    super(message);
  }
}

// This call will fail and throw an error with the message 'This should throw'
invariant(false, new CustomError('This should throw'));