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 /guide/start/introduction.md.

Introduction

byenull is an Option<T> library for JavaScript and TypeScript. It is a companion to the excellent @praha/byethrow, which provides the Result<T, E> type. Use byenull when a value may be absent and byethrow when a failure carries an error. Each library also works on its own.

At a glance

JavaScript lookup APIs often return undefined. Optional chaining and a fallback handle a single lookup directly:

const 
const findUser: (id: string) => User | undefined
findUser
= (
id: string
id
: string) =>
const users: User[]
users
.
Array<User>.find(predicate: (value: User, index: number, obj: User[]) => unknown, thisArg?: any): User | undefined (+1 overload)

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
find
((
user: User
user
) =>
user: User
user
.
id: string
id
===
id: string
id
);
const
const user: User | undefined
user
=
const findUser: (id: string) => User | undefined
findUser
("u123");
const
const name: string
name
=
const user: User | undefined
user
?.
name: string | undefined
name
?? "Unknown";

byenull makes the same possible absence explicit in the return type. A pipeline can then transform the value (if there is one) or supply a default without checking for null or undefined in every step:

import { 
import Option
Option
} from "byenull";
const
const findUser: (id: string) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string) => User | undefined>(fn: (id: string) => User | undefined): (id: string) => Option.Option<User> (+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) =>
const users: User[]
users
.
Array<User>.find(predicate: (value: User, index: number, obj: User[]) => unknown, thisArg?: any): User | undefined (+1 overload)

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

@parampredicate find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.@paramthisArg If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
find
((
user: User
user
) =>
user: User
user
.
id: string
id
===
id: string
id
));
const
const name: string
name
=
import Option
Option
.
function pipe<Option.Option<User>, Option.Option<string>, string>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Option.Option<string>, bc: (b: Option.Option<string>) => string): 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
(
const findUser: (id: string) => Option.Option<User>
findUser
("u123"),
import Option
Option
.
function map<Option.Option<User>, string>(fn: (a: User) => string): (option: Option.Option<User>) => 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: User
user
) =>
user: User
user
.
name: string
name
),
import Option
Option
.
function unwrap<string>(defaultValue: string): <O>(option: O & ([string] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, string, Option.InferSome<O>>

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

@typeParamD - The default value type.@example

Some Case

import { Option } from 'byenull';

const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@example

None Case

import { Option } from 'byenull';

const empty: Option.Option<number> = Option.none();

const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@example

Nullish Default

import { Option } from 'byenull';

const value: number | null = Option.pipe(
Option.some(2),
Option.unwrap(null),
);
@seeorElse - For recovering with a fallback Option instead of a plain value.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryUnwraps
unwrap
("Unknown"),
); // "Alice"

The map callback does not run after None. unwrap returns the value inside Some or the supplied default for None. It does not throw when the Option is empty.

Features

  • Tree-shakable: Every function is an independent export, so your bundle only includes what you use.
  • Plain objects: A Object is a plain object, not a class instance. It is easy to log, serialize, and debug.
  • One API for sync and async: Every function accepts both Ooption<T> and Promise<Option<T>>.
  • Composable: Build readable pipelines with pipe and combinators such as map, andThen, and andThrough.
  • Interop: Use fromResult and toResult to transform byethrow Results to Options and vice versa.
  • Focused: A small set of Option-centric functions, without aliases or confusing variants.
  • Fully type-safe: Every function is covered by type tests, so success and error types are inferred precisely.

Option or Result

Use Option when there might be no value available at the end of a computation and you don't care about errors or failures. This suits array lookups, optional settings, nested optional values and other operations where absence needs no explanation.

Use a Result when the caller needs an error, a cause, or another failure detail to act upon. You can use Option.toResult(error) and Option.fromResult to turn a Option type into a Result and vice versa.

When to Use byenull

byenull works best for values that might be absent as part of your application's normal behavior, such as:

  • API/DB Return Values can be null or undefined
  • Optional settings where you can define a default
  • Collection lookups that might return no value
  • Optional sub objects on your domain types (i.e. a User without an Avatar Image)
  • Business rules such as "missing Avatar image for User" or "the user has no permission"

You don't need to wrap every possible null or undefined in an Option. DOM node lookups, one of operations and similar localized null checks can still be handled with the nullish coalescing operator. See Option vs null and undefined for where to draw that line.