API reference@evolu/commonResult › getOrNull

function getOrNull<T, E>(result: Result<T, E>): T | null;

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

Gets the value from an Ok, or returns null for an Err.

Intended usage:

  • When you need to convert a Result to a nullable value for APIs that expect T | null.
  • When the error is not important and you just want the value or nothing.

Example

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

interface User {
  readonly id: string;
}

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

interface UserNotFoundError extends Typed<"UserNotFound"> {}

// For APIs that expect T | null.
const user = getOrNull(findUser("user-1"));
const missingUser = getOrNull(findUser("missing"));

assertType<User | null, typeof user>();
assertEqual(user, { id: "user-1" });
assertEqual(missingUser, null);