Skip to main content

Result.ok()

Creates a successful result containing a value.

Signatures

Parameters

A
The success value to wrap. If omitted, creates Ok<void, never> for side-effectful operations.

Returns

Ok<A, E>
An Ok instance containing the value

Examples

With a Value

Without a Value (void)

With Explicit Error Type

Result.err()

Creates an error result containing an error value.

Signature

Parameters

E
required
The error value to wrap

Returns

Err<T, E>
An Err instance containing the error

Examples

With a String Error

With a Custom Error Class

With Explicit Success Type

Understanding Phantom Types

Both ok() and err() support phantom type parameters:
Phantom types exist only at compile time for type inference and are erased at runtime. They enable:
  1. Type unification in Result unions
  2. Proper inference in Result.gen() composition
  3. Type safety across multiple operations

Example: Why Phantom Types Matter

Without the phantom types, TypeScript couldn’t verify that both branches return the same Result<number, string> type.

Type Guards

Result.isOk()

Type guard to check if a result is Ok.

Result.isError()

Type guard to check if a result is Err.

Common Patterns

Conditional Creation

Early Returns

With Discriminated Unions

See Also