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/explanation/option-vs-nullish.md.

Option vs null and undefined

TypeScript's strictNullChecks already requires you to handle nullable values. An Option does not remove that obligation. It gives presence and absence a common set of combinators, which helps when several operations depend on the same value.

For an immediate local decision, a nullish value is often simpler. Neither Option nor null | undefined explains why a value is missing.

Use nullish values for local decisions

Optional chaining handles a short lookup without extra imports or wrappers:

type 
type User = {
    profile?: {
        displayName?: string;
    };
}
User
= {
profile?: {
    displayName?: string;
} | undefined
profile
?: {
displayName?: string | undefined
displayName
?: string } };
declare const
const user: User | null
user
:
type User = {
    profile?: {
        displayName?: string;
    };
}
User
| null;
const
const displayName: string
displayName
=
const user: User | null
user
?.
profile?: {
    displayName?: string;
} | undefined
profile
?.
displayName?: string | undefined
displayName
?? "Anonymous";
// displayName is a string

This code has one fallback close to the data it uses. Wrapping user and the nested fields in Options would add work without changing the result.

Plain nullish values also preserve the difference between null and undefined. The some, fn, and maybe creators collapse both into None. That is helpful when both mean "no value", but harmful when a protocol assigns them different meanings. For example, a patch format can use undefined to leave a field unchanged and null to clear it. Do not normalize that distinction away.

Use Options for dependent operations

Consider three lookups where each step needs the previous value. Both versions below are type-safe. They differ in where they handle absence:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
}
User
= {
id: string
id
: string };
type
type Profile = {
    userId: string;
}
Profile
= {
userId: string
userId
: string };
declare function
function findUser(id: string): User | null
findUser
(
id: string
id
: string):
type User = {
    id: string;
}
User
| null;
declare function
function findProfile(user: User): Profile | null
findProfile
(
user: User
user
:
type User = {
    id: string;
}
User
):
type Profile = {
    userId: string;
}
Profile
| null;
declare function
function findAvatar(profile: Profile): string | null
findAvatar
(
profile: Profile
profile
:
type Profile = {
    userId: string;
}
Profile
): string | null;
const
const user: User | null
user
=
function findUser(id: string): User | null
findUser
("u1");
const
const profile: Profile | null
profile
=
const user: User | null
user
=== null ? null :
function findProfile(user: User): Profile | null
findProfile
(
const user: User
user
);
const
const nullableAvatar: string | null
nullableAvatar
=
const profile: Profile | null
profile
=== null ? null :
function findAvatar(profile: Profile): string | null
findAvatar
(
const profile: Profile
profile
);
// nullableAvatar is a string or null const
const findUserOption: (id: string) => Option.Option<User>
findUserOption
=
import Option
Option
.
function fn<(id: string) => User | null>(fn: (id: string) => User | null): (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
(
function findUser(id: string): User | null
findUser
);
const
const findProfileOption: (user: User) => Option.Option<Profile>
findProfileOption
=
import Option
Option
.
function fn<(user: User) => Profile | null>(fn: (user: User) => Profile | null): (user: User) => Option.Option<Profile> (+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
(
function findProfile(user: User): Profile | null
findProfile
);
const
const findAvatarOption: (profile: Profile) => Option.Option<string>
findAvatarOption
=
import Option
Option
.
function fn<(profile: Profile) => string | null>(fn: (profile: Profile) => string | null): (profile: Profile) => Option.Option<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
(
function findAvatar(profile: Profile): string | null
findAvatar
);
const
const avatar: string
avatar
: string =
import Option
Option
.
function pipe<Option.Option<User>, Option.Option<Profile>, Option.Option<string>, string>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Option.Option<Profile>, bc: (b: Option.Option<Profile>) => Option.Option<string>, cd: (c: 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 findUserOption: (id: string) => Option.Option<User>
findUserOption
("u1"),
import Option
Option
.
function andThen<(user: User) => Option.Option<Profile>>(fn: (user: User) => Option.Option<Profile>): <O1>(option: O1) => Option.OptionFor<Option.Option<Profile> | O1, Profile> (+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 findProfileOption: (user: User) => Option.Option<Profile>
findProfileOption
),
import Option
Option
.
function andThen<(profile: Profile) => Option.Option<string>>(fn: (profile: Profile) => Option.Option<string>): <O1>(option: O1) => Option.OptionFor<Option.Option<string> | O1, 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 findAvatarOption: (profile: Profile) => Option.Option<string>
findAvatarOption
),
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
("/default-avatar.png"),
); // A missing value at any step selects the default avatar

The nullable version states each check next to the call it guards. The Option version runs andThen callbacks only for Some and handles every None at unwrap. A fourth dependent lookup adds one andThen step, rather than another variable and null check.

This example illustrates the trade-offs in setup, explicitness and readability. The Option version wraps three existing functions. If nothing else uses those wrappers, the nullable version may be the better choice. The wrappers become more useful when several callers share the same lookups or when the pipeline continues with recovery and transformation steps.

Narrowing works with both representations

With strictNullChecks, TypeScript narrows T | null after a null check. With an Option<T>, isSome narrows Some<T> | None and exposes value:

import { 
import Option
Option
} from "byenull";
declare const
const option: Option.Option<string>
option
:
import Option
Option
.
type Option<T> = Option.Some<T> | Option.None

A union type representing either a Some or a None .

@typeParamT - The type of the Some value.@example
import { Option } from 'byenull';

const doSomething = (): Option.Option<number> => {
  return Math.random() > 0.5
    ? Option.some(10)
    : Option.none();
};
@categoryCore Types
Option
<string>;
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
)) {
const
const uppercased: string
uppercased
: string =
const option: Option.Some<string>
option
.
value: string
value
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
();
var console: Console
console
.
Console.log(...data: any[]): void

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

MDN Reference

log
(
const uppercased: string
uppercased
);
}

The compiler enforces access in both cases. Option's added value is the ability to pass the whole union through map, andThen, and orElse, not a stronger narrowing rule.

The nullish normalization performed by constructors also has limits. Some<T> does not exclude nullish types for hand-built objects, and isOption only checks the object's structure. See Design principles before accepting Option-shaped data from an external source.

Keep the failure reason when callers need it

A nullish result and None can both represent "not loaded", "not found", or "not applicable". Neither records which outcome occurred. If those outcomes need different handling, use a richer return type or a Result with a typed error.

fn and maybe also discard the reason for a Promise rejection. That is appropriate only when every rejection can share the same empty-case handling.

Return an Option when callers will compose operations and treat every empty case alike. Return a nullable value when the caller will check it immediately or needs to distinguish null from undefined. If a value has already been validated, wrapping it only to unwrap it in the next statement adds no useful check.