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/quick.md.

Quick start

This guide walks us through the core workflow of byenull in a few minutes.

Installation

Install the package with your preferred package manager:

npm install byenull
# or: pnpm add byenull
# or: yarn add byenull
# or: bun add byenull

Your first Option

An Option is either a Some that holds a value, or a None that holds nothing. Create them with Option.some and Option.none, and return them instead of null or undefined:

import { 
import Option
Option
} from "byenull";
const
const findUser: (id: string, users: Array<{
    id: string;
    name: string;
}>) => Option.None | Option.Some<{
    id: string;
    name: string;
}>
findUser
= (
id: string
id
: string,
users: {
    id: string;
    name: string;
}[]
users
:
interface Array<T>
Array
<{
id: string
id
: string;
name: string
name
: string }>) => {
const
const user: {
    id: string;
    name: string;
} | undefined
user
=
users: {
    id: string;
    name: string;
}[]
users
.
Array<{ id: string; name: string; }>.find(predicate: (value: {
    id: string;
    name: string;
}, index: number, obj: {
    id: string;
    name: string;
}[]) => unknown, thisArg?: any): {
    id: string;
    name: string;
} | 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: {
    id: string;
    name: string;
}
user
) =>
user: {
    id: string;
    name: string;
}
user
.
id: string
id
===
id: string
id
);
if (
const user: {
    id: string;
    name: string;
} | undefined
user
===
var undefined
undefined
) {
return
import Option
Option
.
function none(): Option.Option<never>

Creates a None : an Option that holds no value.

@returnsA None.@example
import { Option } from 'byenull';

const option = Option.none();
// { type: 'None' }
@categoryCreators
none
();
} return
import Option
Option
.
function some<{
    id: string;
    name: string;
}>(value: {
    id: string;
    name: string;
}): Option.Option<{
    id: string;
    name: 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
(
const user: {
    id: string;
    name: string;
}
user
);
}; const
const users: {
    id: string;
    name: string;
}[]
users
= [{
id: string
id
: "u123",
name: string
name
: "John Doe" }];
const findUser: (id: string, users: Array<{
    id: string;
    name: string;
}>) => Option.None | Option.Some<{
    id: string;
    name: string;
}>
findUser
("u123",
const users: {
    id: string;
    name: string;
}[]
users
);
// { type: 'Some', value: { id: 'u123', name: 'John Doe' } }
const findUser: (id: string, users: Array<{
    id: string;
    name: string;
}>) => Option.None | Option.Some<{
    id: string;
    name: string;
}>
findUser
("u999",
const users: {
    id: string;
    name: string;
}[]
users
);
// { type: 'None' }

To read the value, first check which variant we have with Option.isSome. TypeScript then knows whether value is available:

const 
const users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[] = [{
id: string
id
: "u123",
name: string
name
: "John Doe" }];
const
const user: Option.Option<User>
user
=
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
("u123",
const users: User[]
users
);
if (
import Option
Option
.
function isSome<Option.Option<User>>(option: Option.Option<User>): option is Option.Some<User>

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 user: Option.Option<User>
user
)) {
var console: Console
console
.
Console.log(...data: any[]): void

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

MDN Reference

log
(
const user: Option.Some<User>
user
.
value: User
value
); // { id: 'u123', name: 'John Doe' }
} else {
var console: Console
console
.
Console.log(...data: any[]): void

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

MDN Reference

log
("No such user");
}

Wrapping nullable lookups

Existing code returns null or undefined all the time: find, Map.get, DOM queries. Option.fn wraps such a function so that it returns an Option instead:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
const
const users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[] = [
{
id: string
id
: "u123",
name: string
name
: "John Doe" },
{
id: string
id
: "u456",
name: string
name
: "Jane Doe" },
]; const
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string, users: User[]) => User | undefined>(fn: (id: string, users: User[]) => User | undefined): (id: string, users: User[]) => 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,
users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[]) =>
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 findUser: (id: string, users: User[]) => Option.Option<User>
findUser
("u123",
const users: User[]
users
);
// { type: 'Some', value: { id: 'u123', name: 'John Doe' } }
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
("u999",
const users: User[]
users
);
// { type: 'None' }

Composing steps with pipe

Option.pipe passes a value through a series of steps from top to bottom. Use Option.andThen for steps that can come up empty and return an Option, and Option.map for steps that simply transform the value:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
type
type Article = {
    title: string;
    authorId: string;
}
Article
= {
title: string
title
: string;
authorId: string
authorId
: string };
const
const users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[] = [{
id: string
id
: "u123",
name: string
name
: "John Doe" }];
const
const articles: Article[]
articles
:
type Article = {
    title: string;
    authorId: string;
}
Article
[] = [
{
title: string
title
: "Hello Options",
authorId: string
authorId
: "u123" },
{
title: string
title
: "Goodbye Null",
authorId: string
authorId
: "u123" },
]; const
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string, users: User[]) => User | undefined>(fn: (id: string, users: User[]) => User | undefined): (id: string, users: User[]) => 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,
users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[]) =>
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 findArticlesByAuthor: (authorId: string, articles: Article[]) => Option.Option<Article>
findArticlesByAuthor
=
import Option
Option
.
function fn<(authorId: string, articles: Article[]) => Article | undefined>(fn: (authorId: string, articles: Article[]) => Article | undefined): (authorId: string, articles: Article[]) => Option.Option<Article> (+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
((
authorId: string
authorId
: string,
articles: Article[]
articles
:
type Article = {
    title: string;
    authorId: string;
}
Article
[]) =>
articles: Article[]
articles
.
Array<Article>.find(predicate: (value: Article, index: number, obj: Article[]) => unknown, thisArg?: any): Article | 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
((
article: Article
article
) =>
article: Article
article
.
authorId: string
authorId
===
authorId: string
authorId
),
); const
const article: Option.Option<string>
article
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<User>, Option.Option<string>, Option.Option<Article>, Option.Option<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.Option<string>, cd: (c: Option.Option<string>) => Option.Option<Article>, de: (d: Option.Option<Article>) => Option.Option<string>): Option.Option<...> (+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: any) => Option.Option<User>>(fn: (id: any) => Option.Option<User>): <O1>(option: O1) => Option.OptionFor<Option.Option<User> | O1, User> (+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
((
id: any
id
) =>
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
(
id: any
id
,
const users: User[]
users
)),
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 andThen<(name: any) => Option.Option<Article>>(fn: (name: any) => Option.Option<Article>): <O1>(option: O1) => Option.OptionFor<Option.Option<Article> | O1, Article> (+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
((
name: any
name
) =>
const findArticlesByAuthor: (authorId: string, articles: Article[]) => Option.Option<Article>
findArticlesByAuthor
("u123",
const articles: Article[]
articles
)),
import Option
Option
.
function map<Option.Option<Article>, string>(fn: (a: Article) => string): (option: Option.Option<Article>) => 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
((
found: Article
found
) =>
found: Article
found
.
title: string
title
),
); // article: Option.Option<string> // { type: 'Some', value: 'Hello Options' }

As soon as a step comes up empty, the remaining steps are skipped and the None is passed straight to the end of the pipeline. We don't need to check for absence after every step.

Recovering from an empty result

Use Option.orElse to handle an empty result and continue with a fallback:

const 
const title: Option.Option<string>
title
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<string>>(a: Option.Option<string>, ab: (a: Option.Option<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
(
const article: Option.Option<string>
article
,
import Option
Option
.
function orElse<() => Option.Option<string>>(fn: () => Option.Option<string>): <O1>(option: O1) => Option.OptionFor<Option.Option<string> | O1, string | Option.InferSome<O1>>

Chains 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.

@typeParamF - The fallback function type.@example

None Case

import { Option } from 'byenull';

const option = Option.pipe(
Option.none(),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'default' }
@example

Some Case (fn is never called)

import { Option } from 'byenull';

const option = Option.pipe(
Option.some('value'),
Option.orElse(() => Option.some('default')),
);
// { type: 'Some', value: 'value' }
@seeunwrap - For returning a plain value instead of a fallback Option.@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
orElse
(() =>
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
("Untitled")),
); // { type: 'Some', value: 'Untitled' }

Working with async operations

Async functions work with exactly the same API. When any step returns a Promise, the whole pipeline becomes asynchronous, so we just await the result:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
const
const users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[] = [{
id: string
id
: "u123",
name: string
name
: "John Doe" }];
const
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
=
import Option
Option
.
function fn<(id: string, users: User[]) => User | undefined>(fn: (id: string, users: User[]) => User | undefined): (id: string, users: User[]) => 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,
users: User[]
users
:
type User = {
    id: string;
    name: string;
}
User
[]) =>
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 fetchAvatarUrl: (user: User) => Option.OptionAsync<{
    url: string;
}>
fetchAvatarUrl
=
import Option
Option
.
function fn<(user: User) => Promise<{
    url: string;
} | null>>(fn: (user: User) => Promise<{
    url: string;
} | null>): (user: User) => Option.OptionAsync<{
    url: 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
(async (
user: User
user
:
type User = {
    id: string;
    name: string;
}
User
) => {
const
const response: Response
response
= await
function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
fetch
(`/api/avatars/${
user: User
user
.
id: string
id
}`);
return
const response: Response
response
.
Body.json(): Promise<any>
json
() as
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<{
url: string
url
: string } | null>;
}); const
const avatarUrl: string
avatarUrl
= await
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<User>, Option.OptionAsync<{
    url: string;
}>, Option.OptionAsync<string>, Promise<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.Option<User>, bc: (b: Option.Option<User>) => Option.OptionAsync<{
    url: string;
}>, cd: (c: Option.OptionAsync<{
    url: string;
}>) => Option.OptionAsync<string>, de: (d: Option.OptionAsync<string>) => Promise<string>): Promise<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: any) => Option.Option<User>>(fn: (id: any) => Option.Option<User>): <O1>(option: O1) => Option.OptionFor<Option.Option<User> | O1, User> (+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
((
id: any
id
) =>
const findUser: (id: string, users: User[]) => Option.Option<User>
findUser
(
id: any
id
,
const users: User[]
users
)), // sync
import Option
Option
.
function andThen<(user: User) => Option.OptionAsync<{
    url: string;
}>>(fn: (user: User) => Option.OptionAsync<{
    url: string;
}>): <O1>(option: O1) => Option.OptionFor<Option.OptionAsync<{
    url: string;
}> | O1, {
    url: 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 fetchAvatarUrl: (user: User) => Option.OptionAsync<{
    url: string;
}>
fetchAvatarUrl
), // async
import Option
Option
.
function map<Option.OptionAsync<{
    url: string;
}>, string>(fn: (a: {
    url: string;
}) => string): (option: Option.OptionAsync<{
    url: string;
}>) => Option.OptionAsync<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
((
avatar: {
    url: string;
}
avatar
) =>
avatar: {
    url: string;
}
avatar
.
url: string
url
),
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
("https://example.com/default-avatar.png"),
); // 'https://.../u123.png', or the fallback when anything came up empty

Next steps