Kit
Type aliases

Option

Option<T> = None | Some<T>

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 Option<T> = Some<T> | None;
type Some<T> = { __option: 'Some'; value: T };
type None = { __option: 'None' };

Type Parameters

Type ParameterDescription
TThe 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.

// Create an option with a value.
some('Hello World');
some<number | string>(123);
 
// Create an empty option.
none();
none<number | string>();

See

On this page