API reference@evolu/commonType › nextResult

function nextResult<ValueType, ErrorType, DoneType>(
  valueType: ValidateElement<ValueType>,
  errorType: ValidateElement<ErrorType>,
  doneType: ValidateElement<DoneType>,
): DiscriminatedUnionType<"ok">;

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

Creates a Type for producer Results with value, error, or done outcomes.

The three outcomes are Ok<Value>, Err<Error>, and Err<Typed<"Done"> & { done: Done }>. This keeps normal completion distinct from failure while retaining the ordinary Result shape.

Example

import { assertEqual, String, nextResult, typed } from "@evolu/common";

const StringNextResult = nextResult(
  String,
  typed("ReadFailed", { message: String }),
  String,
);

const describeNext = (input: unknown): string => {
  const validated = StringNextResult.fromUnknown(input);
  if (!validated.ok) return "Invalid result";

  const result = validated.value;
  if (result.ok) return `Value: ${result.value}`;
  if (result.error.type === "Done") return `Done: ${result.error.done}`;
  return `Error: ${result.error.message}`;
};

assertEqual(describeNext({ ok: true, value: "item" }), "Value: item");
assertEqual(
  describeNext({
    ok: false,
    error: { type: "Done", done: "complete" },
  }),
  "Done: complete",
);
assertEqual(
  describeNext({
    ok: false,
    error: { type: "ReadFailed", message: "Offline" },
  }),
  "Error: Offline",
);