For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/modules/Option.md.

Namespace: Option

Re-exports core Option-handling utilities under two convenient namespaces:

  • Option: Verbose and explicit usage
  • O: Shorthand alias for more concise code

Examples

Basic Usage

import { 
import Option
Option
} from 'byenull';
const
const findUser: (id: string) => Option.Option<{
    id: string;
    name: string;
}>
findUser
=
import Option
Option
.
function fn<(id: string) => {
    id: string;
    name: string;
} | null>(fn: (id: string) => {
    id: string;
    name: string;
} | null): (id: string) => Option.Option<{
    id: string;
    name: string;
}> (+1 overload)

Wraps a function and returns a new function that returns an Option or OptionAsync .

Nullish values and Promise rejections produce None ; synchronous throws are not captured.

@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@example

Synchronous Function

import { Option } from 'byenull';

type User = { id: string; name: string };

const findUser = Option.fn((id: string, users: User[]) =>
users.find((user) => user.id === id),
);

findUser('u1', [{ id: 'u1', name: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@example

Asynchronous Function

import { Option } from 'byenull';

type User = { id: string; name: string };

const fetchUser = Option.fn(async (id: string) => {
const response = await fetch(`/api/users/${id}`);
return response.json() as Promise<User>;
});

await fetchUser('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators
fn
((
id: string
id
: string) =>
id: string
id
.
String.startsWith(searchString: string, position?: number): boolean

Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith
('u') ? {
id: string
id
,
name: string
name
: 'John Doe' } : null,
); const
const option: Option.Option<string>
option
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<{
    id: string;
    name: string;
}>, Option.Option<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<{
    id: string;
    name: string;
}>, bc: (b: Option.Option<{
    id: string;
    name: string;
}>) => Option.Option<string>): Option.Option<string> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import Option
Option
.
function some<string>(value: string): Option.Option<string>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
('u123'),
import Option
Option
.
function andThen<(id: string) => Option.Option<{
    id: string;
    name: string;
}>>(fn: (id: string) => Option.Option<{
    id: string;
    name: string;
}>): <O1>(option: O1) => Option.OptionFor<Option.Option<{
    id: string;
    name: string;
}> | O1, {
    id: string;
    name: string;
}> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@example

None Case (fn is never called)

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.andThen((x: number) => Option.some(x * 2)),
);
// { type: 'None' }
@example

Asynchronous Case

import { Option } from 'byenull';

type User = { id: string; name: string };

const fetchUser = Option.fn(async (id: string) =>
fetch(`/api/users/${id}`).then((response) => response.json() as Promise<User>),
);

const option = await Option.pipe(
Option.some('u123'),
Option.andThen(fetchUser),
);
// { type: 'Some', value: User }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(
const findUser: (id: string) => Option.Option<{
    id: string;
    name: string;
}>
findUser
),
import Option
Option
.
function map<Option.Option<{
    id: string;
    name: string;
}>, string>(fn: (a: {
    id: string;
    name: string;
}) => string): (option: Option.Option<{
    id: string;
    name: string;
}>) => Option.Option<string> (+1 overload)

Applies a transformation function to the value of an Option or OptionAsync . If the input is a None , it is returned unchanged.

@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@example

Some Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@example

None Case (unchanged)

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
map
((
user: {
    id: string;
    name: string;
}
user
) =>
user: {
    id: string;
    name: string;
}
user
.
name: string
name
),
); if (
import Option
Option
.
function isSome<Option.Option<string>>(option: Option.Option<string>): option is Option.Some<string>

Type guard to check if an Option is a Some .

@typeParamO - The Option type to check.@paramoption - The Option to check.@returnstrue if the option is a Some, otherwise false.@example
import { Option } from 'byenull';

declare const option: Option.Option<number>;

if (Option.isSome(option)) {
  console.log(option.value); // number
}
@categoryType Guards
isSome
(
const option: Option.Option<string>
option
)) {
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const option: Option.Some<string>
option
.
value: string
value
); // John Doe
}

Shorthand Usage

import { 
import O
O
} from 'byenull';
const
const findUser: (id: string) => O.Option<{
    id: string;
    name: string;
}>
findUser
=
import O
O
.
function fn<(id: string) => {
    id: string;
    name: string;
} | null>(fn: (id: string) => {
    id: string;
    name: string;
} | null): (id: string) => O.Option<{
    id: string;
    name: string;
}> (+1 overload)

Wraps a function and returns a new function that returns an Option or OptionAsync .

Nullish values and Promise rejections produce None ; synchronous throws are not captured.

@typeParamF - The function type to wrap.@returnsA new function that returns an Option or OptionAsync.@example

Synchronous Function

import { Option } from 'byenull';

type User = { id: string; name: string };

const findUser = Option.fn((id: string, users: User[]) =>
users.find((user) => user.id === id),
);

findUser('u1', [{ id: 'u1', name: 'Alice' }]);
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@example

Asynchronous Function

import { Option } from 'byenull';

type User = { id: string; name: string };

const fetchUser = Option.fn(async (id: string) => {
const response = await fetch(`/api/users/${id}`);
return response.json() as Promise<User>;
});

await fetchUser('u1');
// { type: 'Some', value: { id: 'u1', name: 'Alice' } }
@seemaybe - For wrapping a thunk that takes no parameters.@categoryCreators
fn
((
id: string
id
: string) =>
id: string
id
.
String.startsWith(searchString: string, position?: number): boolean

Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith
('u') ? {
id: string
id
,
name: string
name
: 'John Doe' } : null,
); const
const option: O.Option<string>
option
=
import O
O
.
function pipe<O.Option<string>, O.Option<{
    id: string;
    name: string;
}>, O.Option<string>>(a: O.Option<string>, ab: (a: O.Option<string>) => O.Option<{
    id: string;
    name: string;
}>, bc: (b: O.Option<{
    id: string;
    name: string;
}>) => O.Option<string>): O.Option<string> (+25 overloads)

Applies a sequence of functions to a value, from left to right.

This utility enables function composition in a left-to-right manner. It supports up to 25 chained functions from A through Z.

@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@example
import { Option } from 'byenull';

const result = Option.pipe(
  2,
  x => x + 1,
  x => x * 3,
  x => `Result: ${x}`
);
// Result: 9
@categoryUtilities
pipe
(
import O
O
.
function some<string>(value: string): O.Option<string>

Creates a Some from a given value. Passing null or undefined returns None instead.

@typeParamT - The type of the value to wrap.@returnsA Some containing the value, or None if the value is nullish.@example
import { Option } from 'byenull';

const some = Option.some(42);
// { type: 'Some', value: 42 }

const none = Option.some(null);
// { type: 'None' }
@categoryCreators
some
('u123'),
import O
O
.
function andThen<(id: string) => O.Option<{
    id: string;
    name: string;
}>>(fn: (id: string) => O.Option<{
    id: string;
    name: string;
}>): <O1>(option: O1) => O.OptionFor<O.Option<{
    id: string;
    name: string;
}> | O1, {
    id: string;
    name: string;
}> (+1 overload)

Chains the next computation using the value of an Option or OptionAsync . If the input is a None , it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

@typeParamF - The function type to chain.@example

Some Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.some(2),
Option.andThen((x) => Option.some(x * 2)),
);
// { type: 'Some', value: 4 }
@example

None Case (fn is never called)

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.andThen((x: number) => Option.some(x * 2)),
);
// { type: 'None' }
@example

Asynchronous Case

import { Option } from 'byenull';

type User = { id: string; name: string };

const fetchUser = Option.fn(async (id: string) =>
fetch(`/api/users/${id}`).then((response) => response.json() as Promise<User>),
);

const option = await Option.pipe(
Option.some('u123'),
Option.andThen(fetchUser),
);
// { type: 'Some', value: User }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
andThen
(
const findUser: (id: string) => O.Option<{
    id: string;
    name: string;
}>
findUser
),
import O
O
.
function map<O.Option<{
    id: string;
    name: string;
}>, string>(fn: (a: {
    id: string;
    name: string;
}) => string): (option: O.Option<{
    id: string;
    name: string;
}>) => O.Option<string> (+1 overload)

Applies a transformation function to the value of an Option or OptionAsync . If the input is a None , it is returned unchanged.

@typeParamO - The input Option or OptionAsync.@typeParamU - The transformed value type.@example

Some Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@example

None Case (unchanged)

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.map((x: number) => x * 10),
);
// { type: 'None' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
map
((
user: {
    id: string;
    name: string;
}
user
) =>
user: {
    id: string;
    name: string;
}
user
.
name: string
name
),
); if (
import O
O
.
function isSome<O.Option<string>>(option: O.Option<string>): option is O.Some<string>

Type guard to check if an Option is a Some .

@typeParamO - The Option type to check.@paramoption - The Option to check.@returnstrue if the option is a Some, otherwise false.@example
import { Option } from 'byenull';

declare const option: Option.Option<number>;

if (Option.isSome(option)) {
  console.log(option.value); // number
}
@categoryType Guards
isSome
(
const option: O.Option<string>
option
)) {
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const option: O.Some<string>
option
.
value: string
value
); // John Doe
}

Core Types

Type AliasDescription
NoneThe variant of an Option that holds no value.
NonNullableValueA value that is neither null nor undefined.
OptionA union type representing either a Some or a None.
OptionAsyncAn asynchronous variant of Option, wrapped in a Promise.
OptionMaybeAsyncAn option that may be either synchronous or asynchronous.
SomeThe variant of an Option that holds a value.

Infer Types

Type AliasDescription
InferSomeInfers the Some value type T from an Option or a function returning an Option.
OptionForResolves to the appropriate Option type (sync or async) based on the input type.

Creators

FunctionDescription
fnWraps a function and returns a new function that returns an Option or OptionAsync.
maybeExecutes a function and wraps its result in an Option or OptionAsync.
noneCreates a None: an Option that holds no value.
someCreates a Some from a given value. Passing null or undefined returns None instead.

Combinators

FunctionDescription
andThenChains the next computation using the value of an Option or OptionAsync. If the input is a None, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.
inspectExecutes a side effect function on the value of a Some in an Option or OptionAsync, without modifying the original option. This is useful for debugging, logging, or performing other side effects while maintaining the original value.
inspectNoneExecutes a side effect function when an Option or OptionAsync is a None, without modifying the original option. This is useful for debugging, logging, or performing other side effects while maintaining the original value.
mapApplies a transformation function to the value of an Option or OptionAsync. If the input is a None, it is returned unchanged.
orElseChains a fallback computation for an Option or OptionAsync. If the input is a Some, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is.

Unwraps

FunctionDescription
unwrapExtracts the value from an Option or OptionAsync. If the input is a None, the default value is returned.

Type Guards

FunctionDescription
isNoneType guard to check if an Option is a None.
isOptionType guard to check if a value is an Option.
isSomeType guard to check if an Option is a Some.

Interop

FunctionDescription
fromResultConverts a Result from @praha/byethrow into an Option or OptionAsync. A Success becomes a Some; a Failure becomes a None, discarding the error.
toResultConverts an Option or OptionAsync into a Result from @praha/byethrow. A Some becomes a Success; a None becomes a Failure containing err.

Utilities

FunctionDescription
pipeApplies a sequence of functions to a value, from left to right.