All files / guard assert.ts

100% Statements 3/3
100% Branches 3/3
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17                      13x 13x 7x      
/**
 * Throws when a condition is falsy, and narrows its type otherwise.
 *
 * @param condition - The value that must be truthy.
 * @param message - The message of the error. Defaults to `'Assertion failed'`.
 * @throws {Error} When `condition` is falsy.
 * @example
 * assert(user !== undefined, 'User not loaded');
 * user.name; // `user` is no longer `undefined`
 */
export function assert(condition: unknown, message = 'Assertion failed'): asserts condition {
  const isTruthy = Boolean(condition);
  if (!isTruthy) {
    throw new Error(message);
  }
}