Re-exports core Result-handling utilities under two convenient namespaces:
Result: Verbose and explicit usageR: Shorthand alias for more concise codeimport { import Result Result } from '@praha/byethrow';
const const validateId: (id: string) => Result.Failure<Error> | Result.Success<void> validateId = (id: string id : string) => {
if (!id: string id .String.startsWith(searchString: string, position?: number): booleanReturns 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 .const fail: <Error>(error: Error) => Result.Result<never, Error> (+1 overload) fail (new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('Invalid ID format'));
}
return import Result Result .const succeed: () => Result.ResultFor<never, void, never> (+1 overload) succeed ();
};
const const findUser: (id: string) => Result.Result<{
id: string;
name: string;
}, Error>
findUser = import Result Result .try<(id: string) => {
id: string;
name: string;
}, Error>(options: {
try: (id: string) => {
id: string;
name: string;
};
catch: (error: unknown) => Error;
}): (id: string) => Result.Result<{
id: string;
name: string;
}, Error> (+7 overloads)
export try
Wraps a function execution (sync or async) or a Promise in a
Result
or
ResultAsync
type,
capturing errors and returning them in a structured way.
You can use either a custom catch handler or rely on the safe: true option
to assume the function cannot throw.
try ({
try: (id: string) => {
id: string;
name: string;
}
try : (id: string id : string) => {
return { id: string id , name: string name : 'John Doe' };
},
catch: (error: unknown) => Error catch : (error: unknown error ) => new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('Failed to find user', { ErrorOptions.cause?: unknown cause : error: unknown error }),
});
const const result: Result.Result<{
id: string;
name: string;
}, Error>
result = import Result Result .const pipe: <Result.Result<"u123", never>, Result.Result<"u123", Error>, Result.Result<{
id: string;
name: string;
}, Error>>(a: Result.Result<"u123", never>, ab: (a: Result.Result<"u123", never>) => Result.Result<"u123", Error>, bc: (b: Result.Result<"u123", Error>) => Result.Result<{
id: string;
name: string;
}, Error>) => Result.Result<{
id: string;
name: string;
}, Error> (+25 overloads)
pipe (
import Result Result .const succeed: <"u123">(value: "u123") => Result.Result<"u123", never> (+1 overload) succeed ('u123'),
import Result Result .const andThrough: <Result.Result<"u123", never>, Result.Failure<Error> | Result.Success<void>>(fn: (a: "u123") => Result.Failure<Error> | Result.Success<void>) => (result: Result.Result<"u123", never>) => Result.Result<"u123", Error> (+1 overload) andThrough (const validateId: (id: string) => Result.Failure<Error> | Result.Success<void> validateId ),
import Result Result .const andThen: <Result.Result<"u123", Error>, Result.Result<{
id: string;
name: string;
}, Error>>(fn: (a: "u123") => Result.Result<{
id: string;
name: string;
}, Error>) => (result: Result.Result<"u123", Error>) => Result.Result<{
id: string;
name: string;
}, Error> (+1 overload)
andThen (const findUser: (id: string) => Result.Result<{
id: string;
name: string;
}, Error>
findUser ),
);
if (import Result Result .const isSuccess: <{
id: string;
name: string;
}>(result: Result.Result<{
id: string;
name: string;
}, unknown>) => result is Result.Success<{
id: string;
name: string;
}>
Type guard to check if a
Result
is a
Success
.
isSuccess (const result: Result.Result<{
id: string;
name: string;
}, Error>
result )) {
var console: ConsoleThe console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
- A global
console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console .Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format() for more information.
log (const result: Result.Success<{
id: string;
name: string;
}>
result .value: {
id: string;
name: string;
}
value ); // User found: John Doe
}import { import R R } from '@praha/byethrow';
const const validateId: (id: string) => R.Failure<Error> | R.Success<void> validateId = (id: string id : string) => {
if (!id: string id .String.startsWith(searchString: string, position?: number): booleanReturns 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 R R .const fail: <Error>(error: Error) => R.Result<never, Error> (+1 overload) fail (new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('Invalid ID format'));
}
return import R R .const succeed: () => R.ResultFor<never, void, never> (+1 overload) succeed ();
};
const const findUser: (id: string) => R.Result<{
id: string;
name: string;
}, Error>
findUser = import R R .try<(id: string) => {
id: string;
name: string;
}, Error>(options: {
try: (id: string) => {
id: string;
name: string;
};
catch: (error: unknown) => Error;
}): (id: string) => R.Result<{
id: string;
name: string;
}, Error> (+7 overloads)
export try
Wraps a function execution (sync or async) or a Promise in a
Result
or
ResultAsync
type,
capturing errors and returning them in a structured way.
You can use either a custom catch handler or rely on the safe: true option
to assume the function cannot throw.
try ({
try: (id: string) => {
id: string;
name: string;
}
try : (id: string id : string) => {
return { id: string id , name: string name : 'John Doe' };
},
catch: (error: unknown) => Error catch : (error: unknown error ) => new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('Failed to find user', { ErrorOptions.cause?: unknown cause : error: unknown error }),
});
const const result: R.Result<{
id: string;
name: string;
}, Error>
result = import R R .const pipe: <R.Result<"u123", never>, R.Result<"u123", Error>, R.Result<{
id: string;
name: string;
}, Error>>(a: R.Result<"u123", never>, ab: (a: R.Result<"u123", never>) => R.Result<"u123", Error>, bc: (b: R.Result<"u123", Error>) => R.Result<{
id: string;
name: string;
}, Error>) => R.Result<{
id: string;
name: string;
}, Error> (+25 overloads)
pipe (
import R R .const succeed: <"u123">(value: "u123") => R.Result<"u123", never> (+1 overload) succeed ('u123'),
import R R .const andThrough: <R.Result<"u123", never>, R.Failure<Error> | R.Success<void>>(fn: (a: "u123") => R.Failure<Error> | R.Success<void>) => (result: R.Result<"u123", never>) => R.Result<"u123", Error> (+1 overload) andThrough (const validateId: (id: string) => R.Failure<Error> | R.Success<void> validateId ),
import R R .const andThen: <R.Result<"u123", Error>, R.Result<{
id: string;
name: string;
}, Error>>(fn: (a: "u123") => R.Result<{
id: string;
name: string;
}, Error>) => (result: R.Result<"u123", Error>) => R.Result<{
id: string;
name: string;
}, Error> (+1 overload)
andThen (const findUser: (id: string) => R.Result<{
id: string;
name: string;
}, Error>
findUser ),
);
if (import R R .const isSuccess: <{
id: string;
name: string;
}>(result: R.Result<{
id: string;
name: string;
}, unknown>) => result is R.Success<{
id: string;
name: string;
}>
Type guard to check if a
Result
is a
Success
.
isSuccess (const result: R.Result<{
id: string;
name: string;
}, Error>
result )) {
var console: ConsoleThe console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
- A global
console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
console .Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
See util.format() for more information.
log (const result: R.Success<{
id: string;
name: string;
}>
result .value: {
id: string;
name: string;
}
value ); // User found: John Doe
}| Type Alias | Description |
|---|---|
| Failure | Represents a failed result. |
| Result | A union type representing either a success or a failure. |
| ResultAsync | An asynchronous variant of Result, wrapped in a Promise. |
| ResultFor | Resolves to the appropriate Result type (sync or async) based on the input type. |
| ResultMaybeAsync | A result that may be either synchronous or asynchronous. |
| Success | Represents a successful result. |
| Type Alias | Description |
|---|---|
| InferFailure | Infers the Failure value type E from a Result or a function returning a Result. |
| InferSuccess | Infers the Success value type T from a Result or a function returning a Result. |
| Function | Description |
|---|---|
| do | Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines. |
| fail | Creates a Failure result from a given error. Automatically wraps the error in a Promise if it is asynchronous. |
| succeed | Creates a Success result from a given value. Automatically wraps the value in a Promise if it is asynchronous. |
| try | Wraps a function execution (sync or async) or a Promise in a Result or ResultAsync type, capturing errors and returning them in a structured way. |
| Function | Description |
|---|---|
| andThen | Chains the next computation using the success value of a Result or ResultAsync. If the original result is a Failure, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is. |
| andThrough | Runs an additional computation using the success value of a Result or ResultAsync, but returns the original result if the additional computation is successful. |
| bind | Chains another Result-producing computation and merges its success value into the existing object under the specified key. |
| inspect | Executes a side effect function on the success value of a Result or ResultAsync, without modifying the original result. This is useful for debugging, logging, or performing other side effects while maintaining the original value and error state. |
| inspectError | Executes a side effect function on the error value of a Result or ResultAsync, without modifying the original result. This is useful for debugging, logging, or performing other side effects while maintaining the original value and error state. |
| map | Applies a transformation function to the success value of a Result or ResultAsync. If the input is a Failure, it will be returned unchanged. |
| mapError | Applies a transformation function to the error value of a Result or ResultAsync. If the input is a Success, it will be returned unchanged. |
| orElse | Chains the next computation using the error value of a Result or ResultAsync. If the original result is a Success, it is returned unchanged. Otherwise, the provided function is called, and its result is returned as-is. |
| Function | Description |
|---|---|
| unwrap | Extracts the success value from a Result or ResultAsync. |
| unwrapError | Extracts the error value from a Result or ResultAsync. |
| Function | Description |
|---|---|
| assertFailure | Asserts that a Result or ResultAsync is a Failure and returns it. If the result is a Success, throws an error. |
| assertSuccess | Asserts that a Result or ResultAsync is a Success and returns it. If the result is a Failure, throws an error. |
| Function | Description |
|---|---|
| isFailure | Type guard to check if a Result is a Failure. |
| isResult | Type guard to check if a value is a Result. |
| isSuccess | Type guard to check if a Result is a Success. |
| Function | Description |
|---|---|
| collect | Processes multiple Result or ResultAsync values into a single result. If all results are Success, returns a Success containing all values. If any result is a Failure, returns a Failure containing an array of all errors. |
| parse | Parses a value using a Standard Schema compatible schema. Returns a Result with the parsed value on success or validation errors on failure. |
| pipe | Applies a sequence of functions to a value, from left to right. |
| sequence | Processes multiple Result or ResultAsync values into a single result. If all results are Success, returns a Success containing all values. If any result is a Failure, immediately stops processing and returns a Failure with that single error. |