Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Option<T>

The Option type. See the option module documentation for more.

Type parameters

  • T

Hierarchy

  • Option

Implements

Index

Constructors

constructor

  • new Option(value: T, hasSome: boolean): Option

Properties

Protected hasSome

hasSome: boolean

Protected value

value: T

Accessors

isNone

  • get isNone(): boolean
  • Gets true if the option is a None value.

    Examples

    Basic usage:

    const x: Option<number> = Some(2);
    assert.equal(x.isNone, false);
    
    const y: Option<number> = None();
    assert.equal(y.isNone, true);
    

    Returns boolean

isSome

  • get isSome(): boolean
  • Gets true if the option is a Some value.

    Examples

    Basic usage:

    const x: Option<number> = Some(2);
    assert.equal(x.isSome, true);
    
    const y: Option<number> = None();
    assert.equal(y.isSome, false);
    

    Returns boolean

Methods

and

  • Returns None if the option is None, otherwise returns optb.

    Examples

    Basic usage:

    const x1 = Some(2);
    const y1: Option<string> = None();
    assert.deepEqual(x1.and(y1), None());
    
    const x2: Option<number> = None();
    const y2 = Some('foo');
    assert.deepEqual(x2.and(y2), None());
    
    const x3 = Some(2);
    const y3 = Some('foo');
    assert.deepEqual(x3.and(y3), Some('foo'));
    
    const x4: Option<number> = None();
    const y4: Option<string> = None();
    assert.deepEqual(x4.and(y4), None());
    

    Type parameters

    • U

    Parameters

    • optb: Option<U>

      another option

    Returns Option<U>

andThen

  • andThen<U>(cb: function): Option<U>
  • Returns None if the option is None, otherwise calls cb with the wrapped value and returns the result.

    Some languages call this operation flatmap.

    Examples

    Basic usage:

    const sq: (x: number): Option<number> = x => Some(x * x);
    const nope: () => Option<number> = () => None();
    
    assert.deepEqual(Some(2).andThen(sq).andThen(sq), Some(16));
    assert.deepEqual(Some(2).andThen(sq).andThen(nope), None());
    assert.deepEqual(Some(2).andThen(nope).andThen(sq), None());
    assert.deepEqual(None().andThen(sq).andThen(sq), None());
    

    Type parameters

    • U

    Parameters

    • cb: function

      callback to map wrapped value

        • Parameters

          • value: T

          Returns Option<U>

    Returns Option<U>

expect

  • expect(msg: string): T
  • Unwraps an option, yielding the content of a Some.

    Examples

    Basic usage:

    const x = Some('value');
    assert.equal(x.expect('the world is ending'), 'value');
    
    const y: Option<string> = None();
    y.expect('the world is ending'); // throws an error with `the world is ending`
    

    Throws

    Throws an error if the value is a None with a custom error message provided by msg.

    Parameters

    • msg: string

      custom error message

    Returns T

map

  • map<U>(cb: function): Option<U>
  • Maps an Option<T> to Option<U> by applying a function to a contained value.

    Examples

    Basic usage: Convert an Option<string> into an Option<number>, consuming the original:

    const maybeSomeString = Some('Hello, World!');
    const maybeSomeLength = maybeSomeString.map(s => s.length);
    
    assert.deepEqual(maybeSomeLength, Some(13));
    

    Type parameters

    • U

    Parameters

    • cb: function

      callback to map wrapped value

        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns Option<U>

mapOr

  • mapOr<U>(def: U, cb: function): U
  • Applies a function to the contained value (if any), or returns a def (if not).

    Examples

    Basic usage:

    const x = Some('foo');
    assert.equal(x.mapOr(42, v => v.length), 3);
    
    const y: Option<string> = None();
    assert.equal(y.mapOr(42, v => v.length), 42);
    

    Type parameters

    • U

    Parameters

    • def: U

      default value

    • cb: function

      callback to map wrapped value

        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

mapOrElse

  • mapOrElse<U>(def: function, cb: function): U
  • Applies a function to the contained value (if any), or computes a def (if not).

    Examples

    Basic usage:

    const k = 21;
    
    const x = Some('foo');
    assert.equal(x.mapOrElse(() => 2 * k, v => v.length), 3);
    
    const y: Option<string> = None();
    assert.equal(y.mapOrElse(() => 2 * k, v => v.length), 42);
    

    Type parameters

    • U

    Parameters

    • def: function

      callback to calculate default value

        • (): U
        • Returns U

    • cb: function

      callback to map wrapped value

        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

match

  • Matches option to the received matcher and executes the appropriate branch.

    Examples

    Basic usage:

    const x1 = Some(45);
    
    const y1 = x1.match({
      Some: v => v + 5,
      None: () => 10
    });
    
    assert.equal(y1, 50);
    
    const x2 = None();
    
    const y2 = x2.match({
      Some: v => v + 5,
      None: () => 10
    });
    
    assert.equal(y2, 10);
    
    const x3 = Some('foo');
    
    x3.match({
      Some: v => console.log(v), // prints `foo`
      None: () => console.log('nothing')
    });
    

    Type parameters

    • U

    Parameters

    Returns U

okOr

  • okOr<E>(err: E): Result<T, E>
  • Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

    Examples

    Basic usage:

    const x = Some('foo');
    assert.deepEqual(x.okOr(0), Ok('foo'));
    
    const y: Option<string> = None();
    assert.deepEqual(y.okOr(0), Err(0));
    

    Type parameters

    • E

    Parameters

    • err: E

      custom error value

    Returns Result<T, E>

okOrElse

  • okOrElse<E>(err: function): Result<T, E>
  • Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).

    Examples

    Basic usage:

    const x = Some('foo');
    assert.deepEqual(x.okOrElse(() => 0), Ok('foo'));
    
    const y: Option<string> = None();
    assert.deepEqual(y.okOrElse(() => 0), Err(0));
    

    Type parameters

    • E

    Parameters

    • err: function

      callback to calculate custom error value

        • (): E
        • Returns E

    Returns Result<T, E>

or

  • Returns the option if it contains a value, otherwise returns optb.

    Examples

    Basic usage:

    const x1 = Some(2);
    const y1 = None();
    assert.deepEqual(x1.or(y1), Some(2));
    
    const x2 = None();
    const y2 = Some(100);
    assert.deepEqual(x2.or(y2), Some(100));
    
    const x3 = Some(2);
    const y3 = Some(100);
    assert.deepEqual(x3.or(y3), Some(2));
    
    const x4: Option<number> = None();
    const y4 = None();
    assert.deepEqual(x4.or(y4), None());
    assert.deepEqual(x4.or(y4), None());
    

    Parameters

    • optb: Option<T>

      another option

    Returns Option<T>

orElse

  • orElse(cb: function): Option<T>
  • Returns the option if it contains a value, otherwise calls cb and returns the result.

    Examples

    Basic usage:

    const nobody: () => Option<string> = () => None();
    const vikings: () => Option<string> = () => Some('vikings');
    
    assert.deepEqual(Some('barbarians').orElse(vikings), Some('barbarians'));
    assert.deepEqual(None().orElse(vikings), Some('vikings'));
    assert.deepEqual(None().orElse(nobody), None());
    

    Parameters

    • cb: function

      callback to calculate default option

    Returns Option<T>

unwrap

  • unwrap(): T
  • Moves the value v out of the Option<T> if it is Some(v).

    In general, because this function may throw, its use is discouraged. Instead, prefer to use Option.match and handle the None case explicitly.

    Examples

    Basic usage:

    const x = Some('air');
    assert.equal(x.unwrap(), 'air');
    
    const y: Option<string> = None();
    assert.equal(y.unwrap(), 'air'); // fails
    

    Throws

    Throws an error if the self value equals None.

    Returns T

unwrapOr

  • unwrapOr(def: T): T
  • Returns the contained value or a default.

    Examples

    Basic usage:

    assert.equal(Some('car').unwrapOr('bike'), 'car');
    assert.equal(None().unwrapOr('bike'), 'bike');
    

    Parameters

    • def: T

      default value

    Returns T

unwrapOrElse

  • unwrapOrElse(cb: function): T
  • Returns the contained value or computes it from a closure.

    Examples

    Basic usage:

    const k = 10;
    assert.equal(Some(4).unwrapOrElse(() => 2 * k), 4);
    assert.equal(None().unwrapOrElse(() => 2 * k), 20);
    

    Parameters

    • cb: function

      callback to calculate default value

        • (): T
        • Returns T

    Returns T

Generated using TypeDoc