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/tutorial/transforming/mapping-options.md.

Mapping options

map transforms the value inside an Option. It changes the value without adding another way for the pipeline to fail.

Transforming values with map

For a Some, map calls the function and wraps the returned value. For a None, it skips the function and returns the None unchanged:

import { 
import Option
Option
} from "byenull";
const
const option: Option.Option<number>
option
=
import Option
Option
.
function pipe<Option.Option<number>, Option.Option<number>>(a: Option.Option<number>, ab: (a: Option.Option<number>) => Option.Option<number>): Option.Option<number> (+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<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
(2),
import Option
Option
.
function map<Option.Option<number>, number>(fn: (a: number) => number): (option: Option.Option<number>) => Option.Option<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
) =>
value: number
value
* 10),
); // { type: 'Some', value: 20 } const
const empty: Option.Option<number>
empty
=
import Option
Option
.
function pipe<Option.Option<never>, Option.Option<number>>(a: Option.Option<never>, ab: (a: Option.Option<never>) => Option.Option<number>): Option.Option<number> (+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 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
(),
import Option
Option
.
function map<Option.Option<never>, number>(fn: (a: never) => number): (option: Option.Option<never>) => Option.Option<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
* 10),
); // { type: 'None' }, the callback is not called

The callback can change the value type. Mapping a User to user.name, for example, produces an Option<string>.

Normalizing nullish callback results

map passes the callback result through some. A callback that returns null or undefined therefore produces a None:

import { 
import Option
Option
} from "byenull";
type
type Profile = {
    displayName: string | null;
}
Profile
= {
displayName: string | null
displayName
: string | null };
const
const profile: Profile
profile
:
type Profile = {
    displayName: string | null;
}
Profile
= {
displayName: string | null
displayName
: null };
const
const displayName: Option.Option<string | null>
displayName
=
import Option
Option
.
function pipe<Option.Option<Profile>, Option.Option<string | null>>(a: Option.Option<Profile>, ab: (a: Option.Option<Profile>) => Option.Option<string | null>): Option.Option<string | null> (+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<Profile>(value: Profile): Option.Option<Profile>

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 profile: Profile
profile
),
import Option
Option
.
function map<Option.Option<Profile>, string | null>(fn: (a: Profile) => string | null): (option: Option.Option<Profile>) => Option.Option<string | null> (+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
((
profile: Profile
profile
) =>
profile: Profile
profile
.
displayName: string | null
displayName
),
); // { type: 'None' }

some, fn, and maybe apply the same normalization to the values they wrap. fn and maybe also convert Promise rejections to None. Synchronous throws still propagate.

Mapping asynchronous Options

map accepts an OptionAsync. It waits for the input and then runs its synchronous callback on the value:

import { 
import Option
Option
} from "byenull";
const
const product: Promise<Option.Option<{
    name: string;
}>>
product
=
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<Option.Option<{
    name: string;
}>>(value: Option.Option<{
    name: string;
}>): Promise<Option.Option<{
    name: string;
}>> (+2 overloads)

Creates a new resolved promise for the provided value.

@paramvalue A promise.@returnsA promise whose internal state matches the provided promise.
resolve
(
import Option
Option
.
function some<{
    name: string;
}>(value: {
    name: string;
}): Option.Option<{
    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
({
name: string
name
: "Keyboard" }));
const
const name: Option.Option<string>
name
= await
import Option
Option
.
function pipe<Promise<Option.Option<{
    name: string;
}>>, Option.OptionAsync<string>>(a: Promise<Option.Option<{
    name: string;
}>>, ab: (a: Promise<Option.Option<{
    name: string;
}>>) => Option.OptionAsync<string>): Option.OptionAsync<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 product: Promise<Option.Option<{
    name: string;
}>>
product
,
import Option
Option
.
function map<Promise<Option.Option<{
    name: string;
}>>, string>(fn: (a: {
    name: string;
}) => string): (option: Promise<Option.Option<{
    name: 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
((
product: {
    name: string;
}
product
) =>
product: {
    name: string;
}
product
.
name: string
name
),
); // { type: 'Some', value: 'Keyboard' }

Do not pass an async callback to map. The callback would return a Promise as an ordinary value, and map would not wait for the transformation. Use andThen with a callback that returns an Option or OptionAsync instead:

import { 
import Option
Option
} from "byenull";
const
const fetchProduct: (id: string) => Option.OptionAsync<{
    name: string;
}>
fetchProduct
=
import Option
Option
.
function fn<(id: string) => Promise<{
    name: string;
} | null>>(fn: (id: string) => Promise<{
    name: string;
} | null>): (id: string) => Option.OptionAsync<{
    name: 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 (
id: string
id
: string) => {
return
id: string
id
=== "p1" ? {
name: string
name
: "Keyboard" } : null;
}); const
const name: Option.Option<string>
name
= await
import Option
Option
.
function pipe<Option.Option<string>, Option.OptionAsync<{
    name: string;
}>, Option.OptionAsync<string>>(a: Option.Option<string>, ab: (a: Option.Option<string>) => Option.OptionAsync<{
    name: string;
}>, bc: (b: Option.OptionAsync<{
    name: string;
}>) => Option.OptionAsync<string>): Option.OptionAsync<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
("p1"),
import Option
Option
.
function andThen<(id: string) => Option.OptionAsync<{
    name: string;
}>>(fn: (id: string) => Option.OptionAsync<{
    name: string;
}>): <O1>(option: O1) => Option.OptionFor<Option.OptionAsync<{
    name: string;
}> | O1, {
    name: 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 fetchProduct: (id: string) => Option.OptionAsync<{
    name: string;
}>
fetchProduct
),
import Option
Option
.
function map<Option.OptionAsync<{
    name: string;
}>, string>(fn: (a: {
    name: string;
}) => string): (option: Option.OptionAsync<{
    name: 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
((
product: {
    name: string;
}
product
) =>
product: {
    name: string;
}
product
.
name: string
name
),
); // { type: 'Some', value: 'Keyboard' }

Here andThen runs the asynchronous lookup. The following map only formats the resolved product.

Choosing between map and andThen

Use map for a synchronous transformation. Use andThen when the callback is asynchronous and/or can return an absent value wrapped in Option. andThen uses that Option as the next pipeline value instead of nesting it, and a None skips its callback:

import { 
import Option
Option
} from "byenull";
type
type Profile = {
    nickname?: string;
}
Profile
= {
nickname?: string | undefined
nickname
?: string };
const
const profile: Profile
profile
:
type Profile = {
    nickname?: string;
}
Profile
= {
nickname?: string | undefined
nickname
: "Ada" };
const
const getNickname: (profile: Profile) => Option.Option<string>
getNickname
=
import Option
Option
.
function fn<(profile: Profile) => string | undefined>(fn: (profile: Profile) => string | undefined): (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
((
profile: Profile
profile
:
type Profile = {
    nickname?: string;
}
Profile
) =>
profile: Profile
profile
.
nickname?: string | undefined
nickname
);
const
const nested: Option.Option<Option.Option<string>>
nested
=
import Option
Option
.
function pipe<Option.Option<Profile>, Option.Option<Option.Option<string>>>(a: Option.Option<Profile>, ab: (a: Option.Option<Profile>) => Option.Option<Option.Option<string>>): Option.Option<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
(
import Option
Option
.
function some<Profile>(value: Profile): Option.Option<Profile>

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 profile: Profile
profile
),
import Option
Option
.
function map<Option.Option<Profile>, Option.Option<string>>(fn: (a: Profile) => Option.Option<string>): (option: Option.Option<Profile>) => Option.Option<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
(
const getNickname: (profile: Profile) => Option.Option<string>
getNickname
),
); // { type: 'Some', value: { type: 'Some', value: 'Ada' } } const
const flat: Option.Option<string>
flat
=
import Option
Option
.
function pipe<Option.Option<Profile>, Option.Option<string>>(a: Option.Option<Profile>, ab: (a: Option.Option<Profile>) => 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
(
import Option
Option
.
function some<Profile>(value: Profile): Option.Option<Profile>

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 profile: Profile
profile
),
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 getNickname: (profile: Profile) => Option.Option<string>
getNickname
),
); // { type: 'Some', value: 'Ada' }

See How to handle nullish values for ways to wrap nullable APIs before mapping them.