Skip to main content

Overview

better-result provides type utilities to extract success and error types from Result instances. These utilities are essential when working with complex Result types or building generic functions.

InferOk

Extracts the success value type from a Result type.
Result<T, E>
required
The Result type to extract from
T
The success value type T from Result<T, E>

Type Definition

How It Works

InferOk uses a distributive conditional type to extract the success type:
  • For Ok<A, X> | Ok<B, Y>, returns A | B
  • For Result<number, Error>, returns number
  • For non-Result types, returns never

Usage Examples

InferOk is distributive, meaning it distributes over union types. This is particularly useful when working with multiple Result types in generator compositions.

InferErr

Extracts the error value type from a Result type.
Result<T, E>
required
The Result type to extract from
E
The error value type E from Result<T, E>

Type Definition

How It Works

InferErr uses a distributive conditional type to extract the error type:
  • For Err<X, A> | Err<Y, B>, returns A | B
  • For Result<string, NotFoundError>, returns NotFoundError
  • For non-Result types, returns never

Usage Examples

Both InferOk and InferErr work seamlessly with Result.gen to automatically extract union types from all yielded Results.

Common Patterns

Type-Safe Result Handlers

Extracting Types from Complex Workflows

Generic Result Transformers


See Also

Result Core

Learn about Result, Ok, and Err classes

Serialization

Serialize and deserialize Results