index
@ts-rust/std / Result
Result
The Result
module provides utilities for handling operations that can
succeed or fail in a type-safe manner. Inspired by Rust's Result
type, this
module offers the Result
and PendingResult
types to manage success (Ok
)
and failure (Err
) cases explicitly. It includes type definitions for Result
,
PendingResult
, and related error types, as well as utility functions for
creating and checking Result
instances. Use this module to write more
predictable and robust TypeScript code by enforcing explicit error handling.
Enumerations
Enumeration | Description |
---|---|
Enumerates error codes specific to Result operations. These codes categorize failures in ResultError instances thrown by methods such as unwrap or expect when the result’s state (e.g., Ok or Err) doesn’t match the operation’s expectations. |
Classes
Class | Description |
---|---|
An error class for Result operations, extending AnyError with specific ResultErrorKind codes. This class represents failures tied to Result methods, such as accessing
a value from an Err or an error from an Ok. It provides a structured
way to handle such failures by embedding a ResultErrorKind and an optional
Example
|
Interfaces
Interface | Description |
---|---|
Base interface for CheckedError instances, providing methods to inspect and handle the contained error. | |
Interface defining an asynchronous Result that wraps a Extends Result functionality for pending states, with methods mirroring
their synchronous counterparts but returning PendingResult or | |
Interface representing the resultant state of an operation, either a success (Ok\<T\>) or an error (Err\<E\>). Inspired by Rust’s Result, it provides a type-safe alternative to exceptions for handling success or failure outcomes. |
Type Aliases
Type Alias | Description |
---|---|
A type representing the error state of a Result, containing either
an expected error of type This type ensures exactly one error is present: either an expected error
(an anticipated failure of type | |
Represents a failed outcome of a Result, holding a CheckedError
of type This type ensures errors are checked, distinguishing between expected errors
of type | |
Represents an expected error of type | |
Represents a successful outcome of a Result, holding a value
of type | |
A type representing the outcome of an operation, either a success (Ok) or a checked failure (Err). Inspired by Rust’s
Result, this
type provides a type-safe way to handle computations that may succeed with
a value of type | |
A Result type where both the value ( | |
Represents an unexpected error of type ResultError within a CheckedError. |
Functions
Function | Description |
---|---|
Creates an Err variant of a Result with a Wraps Example
| |
Checks if a value is a CheckedError, narrowing its type if true. | |
Checks if a value is a PendingResult, narrowing its type to
This type guard verifies whether the input is a PendingResult,
indicating it wraps a Example
| |
Checks if a value is a Result, narrowing its type to
This type guard verifies whether the input conforms to the Result interface, indicating it is either an Ok or Err. Example
| |
Checks if a value is a ResultError, narrowing its type if true. | |
Creates an Ok variant of a Result with a Wraps Example
| |
Creates a PendingResult\<T, E\> that resolves to Err containing the awaited error. Takes an error or promise and wraps its resolved result in an Err,
ensuring the error type is Example
| |
Creates a PendingResult\<T, E\> that resolves to Ok containing the awaited value. Takes a value or promise and wraps its resolved result in an Ok,
ensuring the value type is Example
| |
Creates a PendingResult\<T, E\> from a result, promise, or factory function. Accepts a Result, a Example
| |
Executes a synchronous action and wraps the outcome in a Result, handling errors with a custom error mapper. The run function attempts to execute the provided This function is useful for safely executing operations that might fail, ensuring errors are handled in a type-safe way using the Result type. Example
| |
Executes an asynchronous action and wraps the outcome in a PendingResult, handling errors with a custom error mapper. The runAsync function attempts to execute the provided This function is useful for safely executing operations that might fail, ensuring errors are handled in a type-safe way using the Result type. Example
| |
Safely executes an action that returns a PendingResult, capturing thrown synchronous errors as an Err variant. The runPendingResult function executes the provided This overload is useful for safely running asynchronous Example
| |
Safely executes an action that returns a Result, capturing thrown synchronous errors as an Err variant. The runResult function executes the provided This function is useful for safely running synchronous Example
|