index
@ts-rust/std / Option
Option
The Option
module provides utilities for handling optional values in
a type-safe manner. Inspired by Rust's Option
type, this module offers the
Option
and PendingOption
types to represent values that may or may not
be present (Some
or None
). It includes type definitions for Option
,
PendingOption
, and related error types, as well as utility functions for
creating and checking Option
instances. Use this module to avoid
null/undefined errors and write more explicit, safer TypeScript code.
Enumerations
Enumeration | Description |
---|---|
Enumerates error codes specific to Option operations. These codes are used in OptionError instances thrown by methods like unwrap or expect when operations fail due to the state of the option. |
Classes
Class | Description |
---|---|
An error thrown by Option methods when operations fail due to the option's state or unexpected conditions. This class extends AnyError with error kinds specific to Option operations, as defined in OptionErrorKind. It is typically thrown by methods like unwrap, expect, or others that enforce strict access or behavior on Some or None variants. Use it to handle failures gracefully in a type-safe manner, inspecting the OptionErrorKind to determine the cause. Example
|
Interfaces
Interface | Description |
---|---|
Interface defining the core functionality of an Option, inspired by Rust's Option type, with additional methods tailored for TypeScript. Represents a value that may or may not be present, offering a robust alternative to
For methods accepting predicates (e.g., orElse, filter, map, andThen), exceptions in the provided function result in None, ensuring predictable, type-safe behavior. If error handling is a concern, use okOr or okOrElse to convert to a Result. Implementations like Some and None enable pattern matching, transformations, and error handling in a type-safe way. | |
Interface defining an asynchronous Option that wraps a Extends Option functionality for pending states, with methods mirroring
their synchronous counterparts but returning PendingOption or |
Type Aliases
Type Alias | Description |
---|---|
Represents an empty Option with no value. Unlike Some, it does not provide a | |
A type that represents either a value (Some\<T\>) or no value (None\<T\>). Inspired by Rust's Option, it is used to handle values that may or may not be present, avoiding null or undefined checks. This is a union of Some and None variants. | |
A synchronous Option where the contained value This restricted Option variant enforces synchronous values for methods like insert, getOrInsert, and getOrInsertWith, which mutate the option. Use it when you need a type-safe, synchronous option. | |
Represents an Option containing a value of type |
Functions
Function | Description |
---|---|
Checks if a value is an Option, narrowing its type to This type guard verifies whether the input conforms to the Optional interface, indicating it is either a Some or None. Example
| |
Checks if a value is an OptionError, narrowing its type if true. | |
Checks if a value is a PendingOption, narrowing its type to
This type guard verifies whether the input is a PendingOption,
indicating it wraps a Example
| |
Creates a None variant of an Option, representing the absence of a value. Produces an option indicating no value is present. Example
| |
Creates a PendingOption\<T\> that resolves to None. Produces a pending option representing the absence of a value, with the type
resolved to Example
| |
Creates a PendingOption\<T\> from an option, promise, or factory function. Accepts an Option, a Example
| |
Creates a PendingOption\<T\> that resolves to Some containing the awaited value. Takes a value or a promise and wraps its resolved result in a Some,
ensuring the value type is Example
| |
Creates a Some variant of an Option containing the given value. Wraps the provided value in a Some, indicating the presence of a value. Example
|