(For a list of methods available on Option<T>, see OptionMethods)
Overview
An Option<T> is a value that represents the existance (or lack thereof) of another value. You can think of it a bit like T | null or T | undefined. While TypeScript can enforce checking to ensure you don't use a null or undefined value, Option provides some additional functionality that can make it a nice alternative.
To create an Option wrapping some value of type T (written as Option<T>), use the Some function. For example: const opt = Some(value). If you want to create an Option representing the lack of a value, use None. For example: const missing = None.
To get to the wrapped value, call unwrap() on the Option. If the option is None, then unwrap() will throw an error; thus, it's important to check to see if an option is Some or None before accessing the value, or use a method that provides a default value in the case of None, like unwrapOr(defaultValue).
Option provides a number of methods for changing an Option into another value and controlling code flow based on its value. By using these methods, we can skip most of the null checking that TypeScript would usually require of us.
Examples
For example, here's an interface representing a theoretical TypeScript type:
interfaceTree { staticgetById(id): Tree | null; staticcreate(id): Tree;
publicgetNode<T>(nodeId): Node<T> | null; }
interfaceNode<T> { publicdata: T | null }
Here's a typical example of how one might interact with this API:
functiongetNodeData<T>(id: string, nodeId: string) { lettree = Tree.getById(id); if (!tree) { tree = Tree.create(id); }
functiongetNodeData<T>(id: string, nodeId: string): T { // Start with an `Option<Tree>` returnTree.getById(id) // If `None`, return `Some(Tree.create(...))` instead .orElse(() =>Some(Tree.create(id))) // Turn the `Option<Tree>` into an `Option<Node>` .andThen(tree=>tree.getNode<T>(nodeId)) // Turn the `Option<Node>` into `Option<T>` .map(node=>node.data) // If `Some`, return the inner value, otherwise return `defaultData` .unwrapOr(defaultData); }
Since calling Some(value) when value is null or undefined returns None, we can easily wrap external APIs that don't use options:
interfaceTree { staticgetById(id): Tree | null; staticcreate(id): Tree;
publicgetNode<T>(nodeId): Node<T> | null; }
interfaceNode<T> { publicdata: T | null }
functiongetNodeData<T>(id: string, nodeId: string): T { // Start with an `Option<Tree>`; this will be `None` if `Tree.getById()` returns `null` returnSome(Tree.getById(id)) // If `None`, return `Some(Tree.create(...))` instead .orElse(() =>Some(Tree.create(id))) // Turn the `Option<Tree>` into an `Option<Node>`; this will be `None` if `getNode()` returns `null` .map(tree=>tree.getNode<T>(nodeId)) // Turn the `Option<Node>` into `Option<T>` .map(node=>node.data) // If `Some`, return the inner value, otherwise return `defaultData` .unwrapOr(defaultData); }
See
OptionMethods for more information on the methods available on Option.
Overview
An
Option<T>
is a value that represents the existance (or lack thereof) of another value. You can think of it a bit likeT | null
orT | undefined
. While TypeScript can enforce checking to ensure you don't use anull
orundefined
value,Option
provides some additional functionality that can make it a nice alternative.To create an
Option
wrapping some value of typeT
(written asOption<T>
), use theSome
function. For example:const opt = Some(value)
. If you want to create anOption
representing the lack of a value, useNone
. For example:const missing = None
.To get to the wrapped value, call
unwrap()
on theOption
. If the option isNone
, thenunwrap()
will throw an error; thus, it's important to check to see if an option isSome
orNone
before accessing the value, or use a method that provides a default value in the case ofNone
, likeunwrapOr(defaultValue)
.Option
provides a number of methods for changing anOption
into another value and controlling code flow based on its value. By using these methods, we can skip most of the null checking that TypeScript would usually require of us.Examples
For example, here's an interface representing a theoretical TypeScript type:
Here's a typical example of how one might interact with this API:
Here's the same example, using an API that returns
Option
s:Since calling
Some(value)
whenvalue
isnull
orundefined
returnsNone
, we can easily wrap external APIs that don't use options:See
OptionMethods for more information on the methods available on
Option
.Type Param: T
The type of the wrapped value