Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

973 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
/**
 * Make an assertion.
 *
 * If the expression is falsy, an error is thrown with the provided message.
 *
 * @example
 * ```ts
 * const value: boolean = Math.random() <= 0.5;
 * invariant(value, "Value is greater than 0.5");
 * value; // true
 * ```
 *
 * @example
 * ```ts
 * const user: { name?: string } = await fetchUser();
 * invariant(user.name, "User missing name");
 * user.name; // string
 * ```
 *
 * @param expression - The condition to check.
 * @param msg - The error message to throw if the assertion fails.
 * @throws {Error} If `expression` is falsy.
 */
export function invariant(
  expression: unknown,
  msg: string,
): asserts expression {
  if (!expression) {
    if (import.meta.env.DEV) {
      // Triggers a breakpoint in development; stripped out in production builds.
      debugger; // oxlint-disable-line no-debugger -- stripped out in production builds
    }
    throw new Error(msg);
  }
}