API reference@evolu/commonResult › getOrThrow

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

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

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 {
  assertEqual,
  assertErr,
  assertInstanceOf,
  assertSame,
  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<typeof config, Config>();
assertEqual(config.port, 3000);

const invalidConfigError: InvalidConfigError = { type: "InvalidConfig" };
const thrown = trySync(() => getOrThrow(err(invalidConfigError)));
assertErr(thrown);
assertInstanceOf(thrown.error, Error);
assertEqual(thrown.error.message, "getOrThrow");
assertSame(thrown.error.cause, invalidConfigError);

Throws: Error with the original error attached as cause.