Option
Index
Accessor
getOrElse
- const x = Some('foo');Option.getOrElse(x, () => 'bar');// 'foo'const x = None;Option.getOrElse(x, () => 'bar');// 'bar'
getOrThrow
Returns the value if
Some, throw an error ifNone⚠ Impure function that may throw an error, its use is generally discouraged.
let x = Some('foo');Option.getOrThrow(x);// 'foo'let x = None;Option.getOrThrow(x);// throw TypeError('option must not be a null|undefined')
Constructor
from
Try to coerce value to
OptionOption.from(null);// undefinedOption.from(undefined);// undefinedOption.from('foo');// 'foo'
Some
An identity function that validates passed value
Type
isNone
Return
trueifanyValueisnullorundefinedOption.isNone(None);// trueOption.isNone(undefined);// trueOption.isNone(null);// trueOption.isNone(Some('foo'));// falseOption.isNone('foo');// false
isSome
Return
trueifanyValueis neithernullnorundefinedOption.isSome(Option.None);// falseOption.isSome(undefined);// falseOption.isSome(null);// falseOption.isSome(Option.Some('foo'));// trueOption.isSome('foo');// true
Other
None
Alias for undefined
Some
Non null and non undefined value
Type parameters
- Value
None
andThen
Returns
Option.Noneif the option isOption.None, otherwise callsfnwith the value and returns the result. Some languages call this operationflatMaporchain.const square = (x: number): Option<number> => Option.Some(x * x);Option.andThen(Option.Some(2), square); // Option.Some(16)Option.andThen(Option.None, square); // Option.None
map
Maps a
Option<Value>toOption<U>by applying a function to a containedSomevalue, leaving aNonevalue untouched. This function can be used to compose the results of two functions.const x = Some('foo');Option.map(x, (value) => `${value}_bar`));// Some('foo_bar') == 'foo_bar'
orElse
Returns the option if it contains a value, otherwise calls
fnand returns the result.const alt = () => Some('bar')Option.orElse(Option.Some('foo'), alt); // Option.Some('foo')Option.orElse(Option.None, alt); // Option.Some('bar')
Returns the
valueifSome,getDefaultValue()ifNone.