Skip to main content
Option<T> models an optional value. It is either Some<T> (a value is present) or None (no value). Under the hood, Option<T> is an alias for Result<NonNullable<T>, never>, so it integrates directly with the rest of the ts-chas ecosystem.

The Some and None types

Because Option is built on Result, all Result methods are available on Option values.

Factory functions

nullable(value)

Returns Some(value) if the value is non-null and non-undefined, otherwise None. This is the most common way to create an Option.

optionFromGuard(value, guard)

Returns Some(value) if the type guard passes, None otherwise. Use this to narrow an unknown value into a typed Option.

some(value)

Explicitly constructs a Some. The value must be non-null and non-undefined.

none()

Explicitly constructs a None.

Converting Option to Result

Option is Result<NonNullable<T>, never>. To assign a concrete error type to the None case, use .orElse():
Use Task.fromOption to lift an Option directly into an async pipeline — see Converting to Task below.

Instance methods

Because Option<T> is a Result, you have the full Result method surface:

.isSome() / .isNone()

Type-narrowing predicates.

.map(fn)

Transforms the value inside Some. Has no effect on None.

.andThen(fn)

flatMap for Options. fn receives the Some value and must return another Option.

.orElse(fn)

Provides a fallback Option if the current one is None.

.unwrapOr(default)

Returns the value if Some, or the default if None.

.unwrap()

Returns the value if Some, or throws if None. Prefer .unwrapOr() or .isSome() checks in production code.

.match({ some, none })

Pattern-matches the option, returning the result of whichever branch applies.
Because Option is a Result, the branches are ok and err — mapping to some and none respectively.

Converting to Task

Use Task.fromOption to lift a synchronous Option into an async Task pipeline. If the option is None, the task fails with the provided error.

Complete example