API reference@evolu/commonType › result

function result<OkType, ErrType>(
  okType: OkType,
  errType: ErrType,
): UnionType<
  [
    ObjectType<{
      ok: LiteralType<true>;
      value: OkType;
    }>,
    ObjectType<{
      error: ErrType;
      ok: LiteralType<false>;
    }>,
  ]
>;

Defined in: packages/common/src/Type.ts:3690

Creates a Type for Result values.

Use for validating serialized Results from storage, APIs, or message passing.

Example

const SyncResponse = result(
  object({ timestamp: NonNegativeInt }),
  typed("SyncError", { message: String }),
);

// Validate response from worker or API
const validated = SyncResponse.from(JSON.parse(message));
if (!validated.ok) return validated; // validation error
// validated.value is Result<{ timestamp }, SyncError>