Overview
functionCore Result

match

Exhaustively handles both result variants and returns one value.

Signature

tsSignature
export function match<T, E, R>(
result: Result<T, E>,
handlers: {
ok: (value: T) => R;
err: (error: E) => R;
},
): R;

Imports

tsImport
import { match } from "@/lib/flow/core/result";
import { match } from "@/lib/flow";

Type Parameters

T

Success value type.

E

Expected error value type.

R

Handler return type.

Parameters

result
Result<T, E>

Result to handle.

handlers
{ ok: (value: T) => R; err: (error: E) => R; }

Callbacks for the success and failure variants.

Returns

The value returned by the matching handler.

Return type
R

Details

`match` keeps success and error handling explicit without manual branching at the call site.

Examples

tsExample
const label = match(result, {
ok: (user) => user.name,
err: (error) => error.kind,
});
err

Re-exports core result helpers used by advanced record internals.

re-export
ok

Re-exports core result helpers used by advanced record internals.

re-export
Result

Explicit success-or-failure outcome used throughout Flow.

type

Source

tsflow/core/result.ts:154-162
export function match<T, E, R>(
result: Result<T, E>,
handlers: {
ok: (value: T) => R;
err: (error: E) => R;
},
): R {
return result.ok ? handlers.ok(result.value) : handlers.err(result.error);
}