Skip to main content

Option

Index

Accessor

getOrElse

  • getOrElse<Value, DefaultValue>(option: Nullable<Value>, getDefaultValue: () => DefaultValue): Value | DefaultValue
  • Returns the value if Some, getDefaultValue() if None.

    @example
    const x = Some('foo');
    Option.getOrElse(x, () => 'bar');// 'foo'

    const x = None;
    Option.getOrElse(x, () => 'bar');// 'bar'

    Type parameters

    • Value
    • DefaultValue

getOrThrow

  • getOrThrow<Value>(option: Nullable<Value>): Value
  • Returns the value if Some, throw an error if None

    ⚠ Impure function that may throw an error, its use is generally discouraged.

    @example
    let x = Some('foo');
    Option.getOrThrow(x);// 'foo'

    let x = None;
    Option.getOrThrow(x);// throw TypeError('option must not be a null|undefined')

    Type parameters

    • Value

Constructor

Some

  • Some<Value>(value: NonNullable<Value>): Option<Value>
  • An identity function that validates passed value

    @example


    Type parameters

    • Value

from

  • from<Value>(value: Value): Option<Exclude<Value, NullableValues>>
  • Try to coerce value to Option

    @example
    Option.from(null);// undefined
    Option.from(undefined);// undefined
    Option.from('foo');// 'foo'

    Type parameters

    • Value

Type

isNone

  • isNone(anyValue: unknown): anyValue is Nullable
  • Return true if anyValue is nullor undefined

    @example
    Option.isNone(None);// true
    Option.isNone(undefined);// true
    Option.isNone(null);// true

    Option.isNone(Some('foo'));// false
    Option.isNone('foo');// false

isSome

  • isSome<Value>(anyValue: Value): anyValue is Exclude<Value, Nullable>
  • Return true if anyValue is neither null nor undefined

    @example
    Option.isSome(Option.None);// false
    Option.isSome(undefined);// false
    Option.isSome(null);// false

    Option.isSome(Option.Some('foo'));// true
    Option.isSome('foo');// true

    Type parameters

    • Value

Other

None

None: undefined

Alias for undefined

Some

Some<Value>: Value extends Nullable ? never : Value

Non null and non undefined value


Type parameters

  • Value

None

None: undefined

andThen

  • andThen<ValueFrom, ValueTo>(option: Nullable<ValueFrom>, fn: (value: ValueFrom) => Nullable<ValueTo>): Option<ValueTo>
  • Returns Option.None if the option is Option.None, otherwise calls fn with the value and returns the result. Some languages call this operation flatMap or chain.

    @example
    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

    Type parameters

    • ValueFrom
    • ValueTo

map

  • map<ValueFrom, ValueTo>(option: Nullable<ValueFrom>, fn: (value: ValueFrom) => NonNullable<ValueTo>): Option<ValueTo>
  • Maps a Option<Value> to Option<U> by applying a function to a contained Some value, leaving a None value untouched. This function can be used to compose the results of two functions.

    @example
    const x = Some('foo');
    Option.map(x, (value) => `${value}_bar`));// Some('foo_bar') == 'foo_bar'

    Type parameters

    • ValueFrom
    • ValueTo

match

  • match<Value, Return>(option: Nullable<Value>, matchers: { None: () => Return; Some: (value: Value) => Return }): Return
  • Return matchers.Some(value) if option is Some, otherwise matchers.None()

    @example
    const stringify = (opt: Option<{foo: string}>) => Option.match(opt, {
    Some: ({ foo }) => foo + '_baz',
    None: () => 'none'
    });

    const someString = stringify(Option.Some({ foo: 'bar' })); // 'bar_baz'
    const noneString = stringify(Option.None); // 'none'

    Type parameters

    • Value
    • Return

orElse

  • orElse<ValueFrom>(option: Nullable<ValueFrom>, fn: () => Nullable<ValueFrom>): Option<ValueFrom>
  • Returns the option if it contains a value, otherwise calls fn and returns the result.

    @example
    const alt = () => Some('bar')

    Option.orElse(Option.Some('foo'), alt); // Option.Some('foo')
    Option.orElse(Option.None, alt); // Option.Some('bar')

    Type parameters

    • ValueFrom