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/resolving/converting-to-results.md.

Converting to Results

An Option represents absence without an error. A Result from @praha/byethrow represents success or failure. Convert between them when the meaning of a missing value changes.

Converting an Option to a Result

toResult(error) supplies the failure value for a None. A Some becomes a Success with the same value:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
const
const present: Option.Option<User>
present
:
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
<
type User = {
    id: string;
    name: string;
}
User
> =
import Option
Option
.
function some<{
    id: string;
    name: string;
}>(value: {
    id: string;
    name: string;
}): Option.Option<{
    id: string;
    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
({
id: string
id
: "u1",
name: string
name
: "Alice" });
const
const missing: Option.Option<User>
missing
:
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
<
type User = {
    id: string;
    name: string;
}
User
> =
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
();
const
const success: Result<User, Error>
success
=
import Option
Option
.
function pipe<Option.Option<User>, Result<User, Error>>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Result<User, Error>): Result<User, Error> (+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 present: Option.Option<User>
present
,
import Option
Option
.
function toResult<Error>(err: Error): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, Error> : Result<Option.InferSome<O>, Error>

Converts an Option or OptionAsync into a Result from

https://github.com/praha-inc/byethrow @praha/byethrow . A Some becomes a Success; a None becomes a Failure containing err.

@typeParamE - The error type for a Failure.@example

Some Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@example

None Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop
toResult
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("User not found")),
); // { type: 'Success', value: { id: 'u1', name: 'Alice' } } const
const failure: Result<User, Error>
failure
=
import Option
Option
.
function pipe<Option.Option<User>, Result<User, Error>>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Result<User, Error>): Result<User, Error> (+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 missing: Option.Option<User>
missing
,
import Option
Option
.
function toResult<Error>(err: Error): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, Error> : Result<Option.InferSome<O>, Error>

Converts an Option or OptionAsync into a Result from

https://github.com/praha-inc/byethrow @praha/byethrow . A Some becomes a Success; a None becomes a Failure containing err.

@typeParamE - The error type for a Failure.@example

Some Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@example

None Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop
toResult
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("User not found")),
); // { type: 'Failure', error: Error('User not found') }

The Option does not contain an error, so the caller must provide one at the conversion point.

Continuing with Result combinators

Once absence becomes a failure, continue with byethrow's combinators. A missing user skips the following Result.map:

import { 
import Option
Option
} from "byenull";
import {
import Result
Result
} from "@praha/byethrow";
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.@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
((
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) => Result.Result<string, Error>
displayName
= (
id: string
id
: string) =>
import Result
Result
.
function pipe<Option.Option<User>, Result<User, Error>, Result.Result<string, Error>>(a: Option.Option<User>, ab: (a: Option.Option<User>) => Result<User, Error>, bc: (b: Result<User, Error>) => Result.Result<string, Error>): Result.Result<string, Error> (+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 26 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 { Result } from '@praha/byethrow';

const result = Result.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 toResult<Error>(err: Error): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, Error> : Result<Option.InferSome<O>, Error>

Converts an Option or OptionAsync into a Result from

https://github.com/praha-inc/byethrow @praha/byethrow . A Some becomes a Success; a None becomes a Failure containing err.

@typeParamE - The error type for a Failure.@example

Some Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@example

None Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop
toResult
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
(`User not found: ${
id: string
id
}`)),
import Result
Result
.
function map<Result<User, Error>, string>(fn: (a: User) => string): (result: Result<User, Error>) => Result.Result<string, Error> (+1 overload)

Applies a transformation function to the success value of a Result or ResultAsync . If the input is a Failure , it will be returned unchanged.

@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@example

Success Case

import { Result } from '@praha/byethrow';

const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@example

Failure Case (unchanged)

import { Result } from '@praha/byethrow';

const result = Result.pipe(
Result.fail('error'),
Result.map((x) => x * 10),
);
// { type: 'Failure', error: 'error' }
@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
),
);
const displayName: (id: string) => Result.Result<string, Error>
displayName
("u1"); // { type: 'Success', value: 'Alice' }
const displayName: (id: string) => Result.Result<string, Error>
displayName
("u404"); // { type: 'Failure', error: Error('User not found: u404') }

Converting a Result to an Option

fromResult converts a Success to a Some and a Failure to a None. It discards the Failure error:

import { 
import Option
Option
} from "byenull";
import {
import Result
Result
} from "@praha/byethrow";
const
const some: Option.Option<2>
some
=
import Option
Option
.
function fromResult<2>(result: Result<2, any>): Option.Option<2> (+1 overload)

Converts a Result from

https://github.com/praha-inc/byethrow @praha/byethrow into an

Option or OptionAsync . A Success becomes a Some ; a Failure becomes a None , discarding the error.

@typeParamT - The success value type.@example

Success Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.succeed(2));
// { type: 'Some', value: 2 }
@example

Failure Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.fail(new Error('Something went wrong')));
// { type: 'None' }
@seetoResult - For the opposite direction.@categoryInterop
fromResult
(
import Result
Result
.
function succeed<2>(value: 2): Result.Result<2, never> (+1 overload)

Creates a Success result from a given value.

Passing a Promise is allowed, but it is recommended to await it before passing to succeed.

@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@example

Synchronous Usage

import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
// Result.Result<42, never>
@example

With No Value

import { Result } from '@praha/byethrow';

const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
succeed
(2));
// { type: 'Some', value: 2 } const
const none: Option.Option<never>
none
=
import Option
Option
.
function fromResult<never>(result: Result<never, any>): Option.Option<never> (+1 overload)

Converts a Result from

https://github.com/praha-inc/byethrow @praha/byethrow into an

Option or OptionAsync . A Success becomes a Some ; a Failure becomes a None , discarding the error.

@typeParamT - The success value type.@example

Success Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.succeed(2));
// { type: 'Some', value: 2 }
@example

Failure Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.fail(new Error('Something went wrong')));
// { type: 'None' }
@seetoResult - For the opposite direction.@categoryInterop
fromResult
(
import Result
Result
.
function fail<Error>(error: Error): Result.Result<never, Error> (+1 overload)

Creates a Failure result from a given error.

Passing a Promise is allowed, but it is recommended to await it before passing to fail.

@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

Synchronous Usage

import { Result } from '@praha/byethrow';

const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@example

With No Value

import { Result } from '@praha/byethrow';

const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
fail
(new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Request failed")));
// { type: 'None' }, the error is discarded

Use this direction only when the caller no longer needs the failure details.

Converting asynchronous values

Both converters accept Promise-based inputs. toResult returns a ResultAsync for an OptionAsync, and fromResult returns an OptionAsync for a ResultAsync:

import { 
import Option
Option
} from "byenull";
import {
import Result
Result
} from "@praha/byethrow";
const
const empty: Option.Option<number>
empty
:
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
<number> =
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
();
const
const result: Result<number, string>
result
= await
import Option
Option
.
function pipe<Promise<Option.Option<number>>, ResultAsync<number, string>>(a: Promise<Option.Option<number>>, ab: (a: Promise<Option.Option<number>>) => ResultAsync<number, string>): ResultAsync<number, 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
(
var Promise: PromiseConstructor

Represents 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
(
const empty: Option.Option<number>
empty
),
import Option
Option
.
function toResult<string>(err: string): <O>(option: O) => true extends Option.HasPromise<O> ? ResultAsync<Option.InferSome<O>, string> : Result<Option.InferSome<O>, string>

Converts an Option or OptionAsync into a Result from

https://github.com/praha-inc/byethrow @praha/byethrow . A Some becomes a Success; a None becomes a Failure containing err.

@typeParamE - The error type for a Failure.@example

Some Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.some(2),
Option.toResult(new Error('No value')),
);
// { type: 'Success', value: 2 }
@example

None Case

import { Option } from 'byenull';

const result = Option.pipe(
Option.none(),
Option.toResult(new Error('No value')),
);
// { type: 'Failure', error: Error('No value') }
@seefromResult - For the opposite direction.@categoryInterop
toResult
("No value"),
); // { type: 'Failure', error: 'No value' } const
const option: Option.Option<never>
option
= await
import Option
Option
.
function fromResult<never>(result: ResultAsync<never, any>): Option.OptionAsync<never> (+1 overload)

Converts a Result from

https://github.com/praha-inc/byethrow @praha/byethrow into an

Option or OptionAsync . A Success becomes a Some ; a Failure becomes a None , discarding the error.

@typeParamT - The success value type.@example

Success Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.succeed(2));
// { type: 'Some', value: 2 }
@example

Failure Case

import { Option } from 'byenull';
import { Result } from '@praha/byethrow';

const option = Option.fromResult(Result.fail(new Error('Something went wrong')));
// { type: 'None' }
@seetoResult - For the opposite direction.@categoryInterop
fromResult
(
var Promise: PromiseConstructor

Represents the completion of an asynchronous operation

Promise
.
PromiseConstructor.resolve<Result.Result<never, "No value">>(value: Result.Result<never, "No value">): Promise<Result.Result<never, "No value">> (+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 Result
Result
.
function fail<"No value">(error: "No value"): Result.Result<never, "No value"> (+1 overload)

Creates a Failure result from a given error.

Passing a Promise is allowed, but it is recommended to await it before passing to fail.

@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

Synchronous Usage

import { Result } from '@praha/byethrow';

const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@example

With No Value

import { Result } from '@praha/byethrow';

const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
fail
("No value")),
); // { type: 'None' }

Convert where absence changes meaning. Option vs Result explains how to choose the type, and How to convert between Option and Result collects boundary patterns.