Option
An implementation of the Rust Option<T> type in JavaScript.
In Rust, optional values are represented using Option<T>, which can be either:
Some(T), indicating a present value.None, indicating the absence of a value.
In JavaScript, this is typically represented as T | null. However, this approach fails with nested options.
For example, Option<Option<T>> in Rust would translate to T | null | null in JavaScript, which is equivalent to T | null.
This means there is no way to differentiate between Some(None) and None, making nested options impossible.
This Option type helps solve this by mirroring Rust’s Option<T> type.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the contained value. |
Example
Here's how you can create Option values.
To improve developer experience, helper functions are available.
TypeScript can infer the type of T or it can be explicitly provided.