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:1271

Asserts that a Result is an Err and narrows it, optionally comparing its error.

When an expected error is provided, it is compared using assertEqual semantics by default. Pass a custom Eq for different equality semantics.

Example

import {
  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, { type: "UserNotFound", id: "user-1" });
assertType<typeof result, Err<UserNotFoundError>>();

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:1274

Asserts that a Result is an Err and narrows it, optionally comparing its error.

When an expected error is provided, it is compared using assertEqual semantics by default. Pass a custom Eq for different equality semantics.

Example

import {
  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, { type: "UserNotFound", id: "user-1" });
assertType<typeof result, Err<UserNotFoundError>>();

Call Signature

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

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

Asserts that a Result is an Err and narrows it, optionally comparing its error.

When an expected error is provided, it is compared using assertEqual semantics by default. Pass a custom Eq for different equality semantics.

Example

import {
  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, { type: "UserNotFound", id: "user-1" });
assertType<typeof result, Err<UserNotFoundError>>();