API reference@evolu/commonAssert › assertOk

Call Signature

function assertOk<R>(result: R): asserts result is Extract<R, Ok<unknown>>;

Defined in: packages/common/src/Assert.ts:198

Asserts that a Result is an Ok, optionally compares its value, and narrows the Result.

With only a Result, this checks the variant without inspecting the value. When an expected value is provided, it uses eqData; its Data type is inferred independently, so an unbranded literal can compare a branded primitive or collection. Pass a custom Eq when either value is outside Data or needs domain-specific equality.

Example

import {
  assertEqual,
  assertOk,
  assertType,
  ok,
  type Ok,
  type Result,
  type Typed,
} from "@evolu/common";

interface User {
  readonly id: string;
}

interface UserNotFoundError extends Typed<"UserNotFound"> {}

const result: Result<User, UserNotFoundError> = ok({ id: "user-1" });

assertOk(result);
assertType<Ok<User>, typeof result>();
assertEqual(result.value.id, "user-1");

Call Signature

function assertOk<R>(
  result: R,
  expectedValue: InferOk<R>,
  eq: Eq<InferOk<R>>,
): asserts result is Extract<R, Ok<unknown>>;

Defined in: packages/common/src/Assert.ts:201

Asserts that a Result is an Ok, optionally compares its value, and narrows the Result.

With only a Result, this checks the variant without inspecting the value. When an expected value is provided, it uses eqData; its Data type is inferred independently, so an unbranded literal can compare a branded primitive or collection. Pass a custom Eq when either value is outside Data or needs domain-specific equality.

Example

import {
  assertEqual,
  assertOk,
  assertType,
  ok,
  type Ok,
  type Result,
  type Typed,
} from "@evolu/common";

interface User {
  readonly id: string;
}

interface UserNotFoundError extends Typed<"UserNotFound"> {}

const result: Result<User, UserNotFoundError> = ok({ id: "user-1" });

assertOk(result);
assertType<Ok<User>, typeof result>();
assertEqual(result.value.id, "user-1");

Call Signature

function assertOk<R, ExpectedValue>(result: R, ...comparison: [ExpectedValue] & IsData<
  | ExpectedValue
  | InferOk<R>> extends true ? unknown : {
assertOk error: Result value and expected value must consist only of Data when no custom Eq is provided.: never;
}): asserts result is Extract<R, Ok<unknown>>;

Defined in: packages/common/src/Assert.ts:207

Asserts that a Result is an Ok, optionally compares its value, and narrows the Result.

With only a Result, this checks the variant without inspecting the value. When an expected value is provided, it uses eqData; its Data type is inferred independently, so an unbranded literal can compare a branded primitive or collection. Pass a custom Eq when either value is outside Data or needs domain-specific equality.

Example

import {
  assertEqual,
  assertOk,
  assertType,
  ok,
  type Ok,
  type Result,
  type Typed,
} from "@evolu/common";

interface User {
  readonly id: string;
}

interface UserNotFoundError extends Typed<"UserNotFound"> {}

const result: Result<User, UserNotFoundError> = ok({ id: "user-1" });

assertOk(result);
assertType<Ok<User>, typeof result>();
assertEqual(result.value.id, "user-1");