Skip to main content

Overview

Result<T, E> is a discriminated union type representing the outcome of an operation that can either succeed with a value of type T or fail with an error of type E.

Type Structure

The Result type is a union of two variants:
  • Ok<T, E> - Represents success with a value of type T
  • Err<T, E> - Represents failure with an error of type E
Both variants use phantom types to enable proper type inference in composition:
  • Ok<T, E>: T is the actual value type, E is a phantom (unused at runtime)
  • Err<T, E>: T is a phantom (unused at runtime), E is the actual error type
This symmetric structure is essential for generator-based composition with Result.gen().

The Ok Variant

Properties

"ok"
required
Discriminant property that identifies this as a success result
A
required
The success value

The Err Variant

Properties

"error"
required
Discriminant property that identifies this as an error result
E
required
The error value

Type Utilities

InferOk

Extracts the success type from a Result:
Example:

InferErr

Extracts the error type from a Result:
Example:

Discriminated Union Pattern

The Result type uses TypeScript’s discriminated union feature via the status property:

Examples

Basic Usage

With Custom Error Types

Pattern Matching

Why Phantom Types?

Phantom types ensure type safety in generator-based composition:
Without phantom types, TypeScript couldn’t infer the union of all possible error types across multiple yields.

See Also