API reference@evolu/commonAssert › assertErr

Call Signature

function assertErr<R>(result: R): asserts result is Extract<R, Err<unknown>>;

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

Asserts that a Result is an Err, optionally compares its error, and narrows the Result.

With only a Result, this checks the variant without inspecting the error. When an expected error 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,
  assertErr,
  assertType,
  err,
  type Err,
  type Result,
  type Typed,
} from "@evolu/common";

interface UserNotFoundError extends Typed<"UserNotFound"> {
  readonly id: string;
}

const result: Result<string, UserNotFoundError> = err({
  type: "UserNotFound",
  id: "user-1",
});

assertErr(result);
assertType<Err<UserNotFoundError>, typeof result>();
assertEqual(result.error.id, "user-1");

Call Signature

function assertErr<R>(
  result: R,
  expectedError: InferErr<R>,
  eq: Eq<InferErr<R>>,
): asserts result is Extract<R, Err<unknown>>;

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

Asserts that a Result is an Err, optionally compares its error, and narrows the Result.

With only a Result, this checks the variant without inspecting the error. When an expected error 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,
  assertErr,
  assertType,
  err,
  type Err,
  type Result,
  type Typed,
} from "@evolu/common";

interface UserNotFoundError extends Typed<"UserNotFound"> {
  readonly id: string;
}

const result: Result<string, UserNotFoundError> = err({
  type: "UserNotFound",
  id: "user-1",
});

assertErr(result);
assertType<Err<UserNotFoundError>, typeof result>();
assertEqual(result.error.id, "user-1");

Call Signature

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

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

Asserts that a Result is an Err, optionally compares its error, and narrows the Result.

With only a Result, this checks the variant without inspecting the error. When an expected error 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,
  assertErr,
  assertType,
  err,
  type Err,
  type Result,
  type Typed,
} from "@evolu/common";

interface UserNotFoundError extends Typed<"UserNotFound"> {
  readonly id: string;
}

const result: Result<string, UserNotFoundError> = err({
  type: "UserNotFound",
  id: "user-1",
});

assertErr(result);
assertType<Err<UserNotFoundError>, typeof result>();
assertEqual(result.error.id, "user-1");