Skip to content

Combine Results: combine, all

Combine utilities — barrel export.

Re-exports result aggregation utilities for combining multiple Result values.

Combines a tuple of results, preserving heterogeneous types. Returns the first failure (with no projected success tuple) or a success tuple.

Like Promise.all but for Result.

Cross-module divergence (intentional). combine/all accepts an empty tuple (readonly IResultOfT<unknown, unknown>[]) and returns Ok([]). The sibling option/all deliberately keeps the stricter readonly [IOption<unknown>, ...IOption<unknown>[]] constraint and rejects the empty tuple. The combine/all constraint was relaxed so that all([]) typechecks; the option/all constraint is left as-is because its tests pin the stricter contract. See combine/all.type-spec.ts for the regression test.

export function all<T extends readonly IResultOfT<unknown, unknown>[]>(
results: T,
): AllResult<T>;

Defined in: combine/all.ts

import { all } from '@sandlada/result/combine';
import { ok } from '@sandlada/result/factories';
all([ok(1), ok('hi'), ok(true)]);
// Ok([1, 'hi', true])

Combines an array of results. Returns the first failure, or a success with all values.

Rust equivalent: Iterator::collect::<Result<Vec<_>, _>>()

Two overloads:

  • Tuple overload (readonly IResultOfT<unknown, unknown>[]): preserves per-position heterogeneous types — combine([ok(1), ok('hi')]) yields IResultOfT<[number, string], never>.
  • Homogeneous array overload (readonly IResultOfT<A, E>[]): collapses to IResultOfT<A[], E>. Useful when the input is a runtime-sized typed array that shares A and E.
export function combine<T extends readonly IResultOfT<unknown, unknown>[]>(
results: readonly [...T],
): IResultOfT<
{ [K in keyof T]: T[K] extends IResultOfT<infer V, unknown> ? V : never },
T[number] extends IResultOfT<unknown, infer E> ? E : never
>;
export function combine<A, E>(
results: readonly IResultOfT<A, E>[],
): IResultOfT<A[], E>;

Defined in: combine/combine.ts

import { combine } from '@sandlada/result/combine';
import { ok, err } from '@sandlada/result/factories';
import type { IResultOfT } from '@sandlada/result';
// Heterogeneous tuple — each position keeps its own type.
combine([ok(1), ok('a')]); // Ok([1, 'a'])
// Homogeneous array.
const arr: IResultOfT<number, string>[] = [ok(1), ok(2), ok(3)];
combine(arr); // Ok([1, 2, 3])
combine([ok(1), err('fail'), ok(3)]); // Err('fail')

Combines results accumulating all errors (validation aggregation). Unlike combine (short-circuit on first failure), this collects every error.

Wlaschin equivalent: &&& (parallel AND in the ROP model)

export function combineWithAllErrors<A, E>(
results: readonly IResultOfT<A, E>[],
): IResultOfT<A[], E[]>;

Defined in: combine/combineWithAllErrors.ts

import { combineWithAllErrors } from '@sandlada/result/combine';
import { ok, err } from '@sandlada/result/factories';
combineWithAllErrors([ok(1), err('a'), err('b')]);
// Err(['a', 'b'])