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/design-principles.md.

Design principles

byenull uses plain data and function pipelines. Those choices affect how you compose code, validate values, and handle asynchronous work. They also have trade-offs that are worth understanding before you adopt the API.

Options are plain objects

An Option is either { type: "Some", value } or { type: "None" }. It has no class instance, private state, or prototype methods. You can inspect it in a debugger and compare it with ordinary object assertions. JSON.stringify can serialize the object when its payload is also serializable. This makes it easy to send Options between server and client.

The trade-off is that values do not have methods such as option.map(...). You import the functions you need and use type guards or pipelines. This keeps the data independent of the operations performed on it.

Constructors normalize nullish values

some(null) and some(undefined) return None. The fn and maybe creators apply the same rule to nullish callback results. Code that consumes these results does not need a second check for a nullish payload.

Normalization is a constructor rule. It is not a guarantee about every object with an Option shape. Some<T> does not constrain T, and isOption checks the object's fields without inspecting the payload:

import { 
import Option
Option
} from "byenull";
const
const handBuilt: Option.Some<null>
handBuilt
:
import Option
Option
.
type Some<T> = {
    readonly type: "Some";
    readonly value: T;
}

The variant of an Option that holds a value.

@typeParamT - The type of the held value.@categoryCore Types
Some
<null> = {
type: "Some"
type
: "Some",
value: null
value
: null };
const
const recognized: boolean
recognized
: boolean =
import Option
Option
.
function isOption<Option.NonNullableValue>(option: unknown): option is Option.Option<Option.NonNullableValue>

Type guard to check if a value is an Option .

@typeParamT - The type of the value.@paramoption - The value to check.@returnstrue if the value is an Option, otherwise false.@example
import { Option } from 'byenull';

const value: unknown = { type: 'Some', value: 42 };

if (Option.isOption<number>(value)) {
  // value is Option<number>
}
@categoryType Guards
isOption
(
const handBuilt: Option.Some<null>
handBuilt
);
// recognized is true, even though the value is null

Prefer the creators when you want nullish normalization. If an Option-shaped object comes from an untrusted source, validate its Some.value separately before treating it as non-nullish.

Functions take data last

A call such as Option.map(fn) returns a function that waits for an Option. This makes the same function usable as a step in Option.pipe:

import { 
import Option
Option
} from "byenull";
const
const doubled: Option.Option<number>
doubled
=
import Option
Option
.
function map<number, number>(fn: (a: number) => number): <O2>(option: O2) => Option.OptionFor<O2, number> (+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
((
value: number
value
: number) =>
value: number
value
* 2)(
import Option
Option
.
function some<number>(value: number): Option.Option<number>

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
(21));
const
const domain: string
domain
=
import Option
Option
.
function pipe<Option.Option<string>, Option.Option<string>, string>(a: Option.Option<string>, ab: (a: Option.Option<string>) => 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
(
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
("alice@example.com"),
import Option
Option
.
function map<Option.Option<string>, string>(fn: (a: string) => string): (option: Option.Option<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
((
address: string
address
) =>
address: string
address
.
String.slice(start?: number, end?: number): string

Returns a section of a string.

@paramstart The index to the beginning of the specified portion of stringObj.@paramend The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. If this value is not specified, the substring continues to the end of stringObj.
slice
(
address: string
address
.
String.indexOf(searchString: string, position?: number): number

Returns the position of the first occurrence of a substring, or -1 if it is not present.

@paramsearchString The substring to search for in the string@paramposition The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.
indexOf
("@") + 1)),
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"),
); // domain is a string

A single transformation has needs a separate call of the applied function, as doubled shows. A pipeline lists later steps in execution order, which avoids nesting a call around each preceding result. byenull uses this convention throughout instead of supporting both data-first and data-last variants.

Synchronous throws and Promise rejections differ

fn and maybe turn a nullish callback result into None. When the callback returns a Promise, they also turn a rejection into None. A synchronous throw before the callback returns a Promise propagates to the caller.

A throw inside an async function becomes a Promise rejection, so fn and maybe convert it to None. This distinction follows JavaScript's execution rules. It is not based on the kind of error thrown.

Converting a rejection to None discards its reason. Use a Result when the caller needs the rejection value for logging, recovery, or a user-facing message.

byenull follows byethrow where their behaviors overlap

byenull uses the namespace style, curried data-last functions, pipe, and Promise-aware combinators from @praha/byethrow. Code that uses both libraries can keep the same pipeline structure.

The two types still carry different information. None has no error field, while a byethrow Failure carries an error. toResult therefore requires an error for the empty case, and fromResult discards a failure's error. The shared conventions do not hide that difference.

byenull is meant as a companion library to byethrow, though you can use either one exclusively.