API reference@evolu/commonResult › getOrThrow

function getOrThrow<T, E>(result: Result<T, E>): T;

Defined in: packages/common/src/Result.ts:476

Gets the value from an Ok, or throws for an Err.

Use this where failure should crash the current flow instead of being handled locally.

When to use:

  • Application startup or composition-root setup where errors must stop the program immediately. In Evolu apps, the root Run reports the defect and the platform lifecycle API handles shutdown.
  • Module-level constants
  • Test setup with values that are expected to be valid

Prefer an explicit if (!result.ok) check in ordinary application logic where the caller can recover, retry, or choose a different flow.

Example

import {
  assert,
  assertErr,
  assertEqual,
  assertType,
  err,
  getOrThrow,
  ok,
  trySync,
  type Result,
  type Typed,
} from "@evolu/common";

interface Config {
  readonly port: number;
}

const loadConfig = (): Result<Config, InvalidConfigError> => ok({ port: 3000 });

interface InvalidConfigError extends Typed<"InvalidConfig"> {}

// At app startup, crash if the config is invalid.
const config = getOrThrow(loadConfig());
assertType<Config, typeof config>();
assertEqual(config.port, 3000);

const thrown = trySync(() => getOrThrow(err({ type: "InvalidConfig" })));
assertErr(thrown);
assert(thrown.error instanceof Error, "Expected an Error.");
assertEqual(thrown.error.message, "getOrThrow");
const cause = thrown.error.cause;
assert(
  typeof cause === "object" &&
    cause !== null &&
    "type" in cause &&
    cause.type === "InvalidConfig",
  "Expected InvalidConfig cause.",
);

Throws: Error with the original error attached as cause.