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/option-vs-result.md.

Option vs Result

byenull and @praha/byethrow represent different outcomes. An Option represents a computation that can have no value. A Result represent a computation that can fail. Choose between them by deciding what the caller needs to know and how it can respond.

Use Option when absence needs no explanation

A lookup can legitimately return nothing without providing an error:

import { 
import Option
Option
} from "byenull";
type
type User = {
    id: string;
    name: string;
}
User
= {
id: string
id
: string;
name: string
name
: string };
declare const
const users: readonly User[]
users
: readonly
type User = {
    id: string;
    name: string;
}
User
[];
const
const findUser: (id: string) => Option.Option<User>
findUser
= (
id: string
id
: string):
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<User | undefined>(value: User | undefined): Option.Option<User>

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 users: readonly User[]
users
.
ReadonlyArray<User>.find(predicate: (value: User, index: number, obj: readonly 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
));

A search screen might display the same empty state for every missing user. None is enough for that decision. It has no message, cause, or error type, so it does not explain why the lookup returned nothing.

Absence is not inherently separate from failure. The domain determines which model fits. A missing user can be an ordinary search result in one function and a failed authentication requirement in another.

Use Result for anticipated failures with useful errors

A Failure carries an error. Its type can tell the caller which anticipated failures need handling:

import { 
import Result
Result
} from "@praha/byethrow";
class
class InvalidUserIdError
InvalidUserIdError
extends
var Error: ErrorConstructor
Error
{}
const
const parseUserId: (input: string) => Result.Result<string, InvalidUserIdError>
parseUserId
= (
input: string
input
: string):
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<string,
class InvalidUserIdError
InvalidUserIdError
> => {
if (!
input: string
input
.
String.startsWith(searchString: string, position?: number): boolean

Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

startsWith
("u_")) {
return
import Result
Result
.
function fail<InvalidUserIdError>(error: InvalidUserIdError): Result.Result<never, InvalidUserIdError> (+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
constructor InvalidUserIdError(message?: string, options?: ErrorOptions): InvalidUserIdError (+1 overload)
InvalidUserIdError
("A user ID must start with u_"));
} return
import Result
Result
.
function succeed<string>(value: string): Result.Result<string, 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
(
input: string
input
);
};

This is useful when the caller can show a validation message, return a specific HTTP response, retry a defined operation, or combine several typed errors. The error stays available for those decisions.

Expected file, network, and parsing failures can fit this model. An operation does not need a Result merely because it performs I/O or uses code that can throw. The caller's response is the deciding factor.

Let unexpected failures throw or reject

Some failures indicate a bug or a problem that only top-level infrastructure can report. Examples include reading a property from an incorrectly initialized object or losing a required database connection. If the caller cannot respond differently to these failures, adding them to a broad Result<T, Error> return type creates handling code without giving the caller a useful choice.

Let those failures throw or reject, then report them at the application boundary. This follows the same division used in byethrow's error-handling guidance: anticipated failures that callers handle belong in Result, while unexpected failures can remain exceptions.

Be careful when wrapping asynchronous work with Option.fn or Option.maybe. They convert every Promise rejection to None and discard the error. Use them only when that loss is acceptable.

Convert where the caller's needs change

toResult(error) turns Some into Success and None into Failure with the supplied error. Since None contains no information, the call site must provide an error that fits its context.

fromResult turns Success into Some and every Failure into None. It discards the error:

import { 
import Option
Option
} from "byenull";
import {
import Result
Result
} from "@praha/byethrow";
type
type Config = {
    host: string;
}
Config
= {
host: string
host
: string };
declare const
const optionalConfig: Option.Option<Config>
optionalConfig
:
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 Config = {
    host: string;
}
Config
>;
declare const
const loadedConfig: Result.Result<Config, Error>
loadedConfig
:
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<
type Config = {
    host: string;
}
Config
, Error>;
const
const requiredConfig: Result<Config, Error>
requiredConfig
=
import Option
Option
.
function pipe<Option.Option<Config>, Result<Config, Error>>(a: Option.Option<Config>, ab: (a: Option.Option<Config>) => Result<Config, Error>): Result<Config, 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 optionalConfig: Option.Option<Config>
optionalConfig
,
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
("The application requires a config")),
); const
const cachedConfig: Option.Option<Config>
cachedConfig
:
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 Config = {
    host: string;
}
Config
> =
import Option
Option
.
function fromResult<Config>(result: Result<Config, any>): Option.Option<Config> (+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
(
const loadedConfig: Result.Result<Config, Error>
loadedConfig
);
// A Failure's Error is no longer available in cachedConfig

Handle or log a Failure before fromResult if its error still matters. Both converters handle asynchronous inputs. Await their results when the input is asynchronous.

Keep conversions near the decision that changes the return type. An optional cache lookup can remain an Option. If startup cannot continue without that value, convert once to a Result with a startup-specific error. Converting every lookup before that point forces absence-only code to handle errors it does not need.