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

Why byenull?

TypeScript's strictNullChecks configuration already prevents direct property access on a nullable value. Keep using ?. and ?? for a local property or a single default. Byenull shines when you have more complex operations that operate on values that might be missing.

The problem with null and undefined

JavaScript has a lot of historical baggage. Among that baggage we find two ways to declare the absence of a value and messy identity checks.

Most JavaScript developers treat null, undefined and field? as the same thing, when in fact they aren't:

null == 
var undefined
undefined
// true
null ===
var undefined
undefined
// false
typeof null // 'object' typeof
var undefined
undefined
// 'undefined'

Defining complex structures therefore can get very messy quite easily.

type 
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: string | null,
avatar?: string | null | undefined
avatar
?: string | null,
age: number | undefined
age
: number | undefined }
// These are all valid shapes of a User const
const u1: User
u1
:
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: "John Doe",
age: number | undefined
age
:
var undefined
undefined
}
const
const u2: User
u2
:
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: "Jane Doe",
avatar?: string | null | undefined
avatar
: null,
age: number | undefined
age
:
var undefined
undefined
}
const
const u3: User
u3
:
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: null,
avatar?: string | null | undefined
avatar
:
var undefined
undefined
,
age: number | undefined
age
:
var undefined
undefined
}
const
const u4: User
u4
:
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: null,
age: number | undefined
age
:
var undefined
undefined
}
// This shape is rejected: 'age' is required const u5:
type User = {
    name: string | null;
    avatar?: string | null;
    age: number | undefined;
}
User
= {
name: string | null
name
: "John Doe"}
Property 'age' is missing in type '{ name: string; }' but required in type 'User'.

With undefined, null and ? in the mix, the typechecker allows multiple shapes of a user object.

This can lead to a lot of subtle bugs, incoherent value absence handling and mapping issues, especially when talking to external APIs, i.e. your database, where undefined is not a concept, null might mean something different and missing object keys (because they defined with fieldName?) cause errors in queries.

Furthermore, if you declare a variable var a and don't explicitly set a value, it's initial value is undefined.

byenull makes absence explicit

import { 
import O
O
} from "byenull";
type
type User = {
    name: O.Option<string>;
    avatar: O.Option<string>;
    age: O.Option<number>;
}
User
= {
name: O.Option<string>
name
:
import O
O
.
type Option<T> = O.Some<T> | O.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>,
avatar: O.Option<string>
avatar
:
import O
O
.
type Option<T> = O.Some<T> | O.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>,
age: O.Option<number>
age
:
import O
O
.
type Option<T> = O.Some<T> | O.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
<number> }
const
const u1: User
u1
:
type User = {
    name: O.Option<string>;
    avatar: O.Option<string>;
    age: O.Option<number>;
}
User
= {
name: O.Option<string>
name
:
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
("John Doe"),
avatar: O.Option<string>
avatar
:
import O
O
.
function none(): O.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
(),
age: O.Option<number>
age
:
import O
O
.
function none(): O.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
()}
const
const u2: User
u2
:
type User = {
    name: O.Option<string>;
    avatar: O.Option<string>;
    age: O.Option<number>;
}
User
= {
name: O.Option<string>
name
:
import O
O
.
function none(): O.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
(),
avatar: O.Option<string>
avatar
:
import O
O
.
function none(): O.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
(),
age: O.Option<number>
age
:
import O
O
.
function none(): O.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
()}

Option tells the compiler explicitly that a value might be absent in one coherent way. Setting all fields is required reducing the amount of allowed shapes to 1.

Keeping your business logic uniform and reducing the number of ways to do things is important for writing maintainable code, no matter if it is written by humans or agents. byenull gives you one uniform toolset to handle the absence of a value.

Option is not for error handling

None has no error or explanation. byenull also normalizes both null and undefined to None, so it cannot preserve a distinction between them. Keep a nullable value or define a more specific union when that distinction matters (i.e. forms in HTML produce behave differently on undefined and null).

Most importantly None cannot act as a error handling solution, since it does not hold any information on why a value might be absent.

Pair absence with failure

byenull is designed to work well with the excellent @praha/byethrow library, which provides a Result<T, E> type to make failures explicit. Return an Option when "not found" or "not set" needs no error. Return a Result when parsing fails, a request returns an error that the caller must inspect, or a business rule needs a typed failure reason.

Both libraries use namespaces and familiar operation names such as map, andThen, orElse, and unwrap, so if you already use one the two, you'll feel right at home with the other library.

Why byenull over other libraries?

There are several Option libraries in the TypeScript ecosystem. byenull aims for the sweet spot between a minimal utility and a full functional programming framework.

Compared to other libraries

Most Option libraries are direct ports from Rusts Option<T> enum. These libraries often use classes for the Option type and their ergonomics falls apart when mixing synchronous and async operations. byenull makes different trade-offs:

  • Plain objects: A byenull Option is a plain { type, value } or { type } object that can be logged, serialized, and compared as it is.
  • Standalone functions: Operations are independent functions combined with pipe, so unused ones are removed by tree-shaking.
  • One API for sync and async: The same functions work with both Option and Promise<Option>, so you never need to convert between two kinds of results.
  • Interop with byethrow: Functions such as fromResult and toResult allow for easy interop between the Result and Option type.

Compared to Effect and fp-ts

Effect and fp-ts are extremely powerful, but they come with their own runtime model and a large vocabulary of functional programming concepts. byenull only deals with some value or no value, so your team can adopt it without learning a new programming paradigm.

Designed for Real-World Use

  • Team-friendly: Adopt it one function at a time. It works alongside existing code that throws.
  • TypeScript-first: Some types are inferred through every step of a pipeline.
  • Lightweight: A small runtime with full tree-shaking support.
  • Predictable: A consistent naming scheme (map, andThen / orElse, and so on) keeps the API easy to remember.
  • Well-equipped: A linter plugin, test matchers, and documentation for AI assistants help the whole team follow the same patterns.