#Unwrapping options
Code outside an Option pipeline usually needs a plain value. unwrap returns the value inside a Some or a supplied default for a None.
#Extracting values with unwrap
Pass the default to unwrap, then use it as the last step in a pipeline:
import { import Option Option } from "byenull";
const const value: number value = import Option Option .function pipe<Option.Option<number>, number>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number): 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.@exampleimport { 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (42),
import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (0),
);
// 42
const const empty: Option.Option<number> empty : import Option Option .type Option<T> = Option.Some<T> | Option.NoneA union type representing either a
Some
or a
None
.
@typeParamT - The type of the Some value.@exampleimport { Option } from 'byenull';
const doSomething = (): Option.Option<number> => {
return Math.random() > 0.5
? Option.some(10)
: Option.none();
};
@categoryCore Types Option <number> = import Option Option .function none(): Option.Option<never>Creates a
None
: an Option that holds no value.
@returnsA None.@exampleimport { Option } from 'byenull';
const option = Option.none();
// { type: 'None' }
@categoryCreators none ();
const const fallback: number fallback = import Option Option .function pipe<Option.Option<number>, number>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number): 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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (const empty: Option.Option<number> empty , import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (0));
// 0A None never causes unwrap to throw. It always returns the default.
#Choosing the default type
The default must be assignable to the Option's value type, or be null or undefined:
import { import Option Option } from "byenull";
const const option: Option.Option<number> option = 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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (2);
const const value: number value = import Option Option .function pipe<Option.Option<number>, number>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number): 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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (const option: Option.Option<number> option , import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (0));
// number
const const nullable: number | null nullable = import Option Option .function pipe<Option.Option<number>, number | null>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number | null): number | 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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (const option: Option.Option<number> option , import Option Option .function unwrap<null>(defaultValue: null): <O>(option: O & ([null] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, null, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (null));
// number | null
const const optional: number | undefined optional = import Option Option .function pipe<Option.Option<number>, number | undefined>(a: Option.Option<number>, ab: (a: Option.Option<number>) => number | undefined): number | undefined (+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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (const option: Option.Option<number> option , import Option Option .function unwrap<undefined>(defaultValue: undefined): <O>(option: O & ([undefined] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, undefined, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (var undefined undefined ));
// number | undefinedTypeScript rejects an incompatible non-nullish default. Nullish defaults are useful when the calling code already accepts a nullable or optional result.
#Unwrapping in a pipeline
Keep transformations inside the pipeline, then extract the value once:
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 : "u1", name: string name : "Alice" }];
const const findUser: (id: string) => Option.Option<User> findUser = import Option Option .function fn<(id: string) => User | undefined>(fn: (id: string) => User | undefined): (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.@exampleSynchronous 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' } }
@exampleAsynchronous 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) => const 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 displayName: (id: string) => string displayName = (id: string id : string): string =>
import Option Option .function pipe<Option.Option<User>, Option.Option<string>, string>(a: Option.Option<User>, ab: (a: Option.Option<User>) => 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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const findUser: (id: string) => Option.Option<User> findUser (id: string id ),
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.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@exampleNone 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 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 ("Guest"),
);
const displayName: (id: string) => string displayName ("u1"); // 'Alice'
const displayName: (id: string) => string displayName ("u404"); // 'Guest'After unwrap, the result is a plain string, so later Option combinators cannot be applied to it.
#Unwrapping asynchronous Options
unwrap accepts an OptionAsync and returns a Promise of the value or default:
import { import Option Option } from "byenull";
const const value: number value = await import Option Option .function pipe<Promise<Option.Option<number>>, Option.OptionAsync<number>, Promise<number>>(a: Promise<Option.Option<number>>, ab: (a: Promise<Option.Option<number>>) => Option.OptionAsync<number>, bc: (b: Option.OptionAsync<number>) => Promise<number>): Promise<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.@exampleimport { Option } from 'byenull';
const result = Option.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
var Promise: PromiseConstructorRepresents the completion of an asynchronous operation
Promise .PromiseConstructor.resolve<Option.Option<number>>(value: Option.Option<number>): Promise<Option.Option<number>> (+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<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.@exampleimport { Option } from 'byenull';
const some = Option.some(42);
// { type: 'Some', value: 42 }
const none = Option.some(null);
// { type: 'None' }
@categoryCreators some (21)),
import Option Option .function map<Promise<Option.Option<number>>, number>(fn: (a: number) => number): (option: Promise<Option.Option<number>>) => Option.OptionAsync<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.@exampleSome Case
import { Option } from 'byenull';
const option = Option.pipe(
Option.some(2),
Option.map((x) => x * 10),
);
// { type: 'Some', value: 20 }
@exampleNone 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 * 2),
import Option Option .function unwrap<number>(defaultValue: number): <O>(option: O & ([number] extends [Option.InferSome<O> | null | undefined] ? unknown : never)) => Option.Unwrapped<O, number, 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.@exampleSome Case
import { Option } from 'byenull';
const value = Option.pipe(
Option.some(2),
Option.unwrap(0),
);
// 2
@exampleNone Case
import { Option } from 'byenull';
const empty: Option.Option<number> = Option.none();
const value = Option.pipe(
empty,
Option.unwrap(0),
);
// 0
@exampleNullish 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 (0),
);
// 42#Where to unwrap
Unwrap at application boundaries, such as when rendering a response or returning a value from a CLI command. Keep the Option while more map, andThen, or orElse steps need to run.
A default says that absence is acceptable. If a missing value must be handled as an error, convert the Option to a Result instead of hiding it with a default. Option vs null and undefined discusses when plain nullish handling is clearer.