Options
All
  • Public
  • Public/Protected
  • All
Menu

Represents optional values, inspired by Scala's Option and by Haskell's Maybe data types.

Option is an immutable data type, represented as a sum type, being either a Some, in case it contains a single element, or a None, in case it is empty.

The most idiomatic way to use an Option instance is to treat it as a collection or monad and use map,flatMap, filter, or forEach.

final

Type parameters

  • A

Hierarchy

Implements

Index

Constructors

Protected constructor

  • new Option(ref: A | undefined, isEmpty?: undefined | true | false): Option

Properties

value

value: undefined | A

Methods

ap

  • Applicative apply operator.

    Resembles map, but the passed mapping function is lifted in the Either context.

    Type parameters

    • B

    Parameters

    Returns Option<B>

chain

  • chain<B>(f: function): Option<B>

contains

  • contains(elem: A): boolean
  • Returns true if this option is nonempty and the value it holds is equal to the given elem.

    Parameters

    • elem: A

    Returns boolean

equals

  • equals(that: Option<A>): boolean

exists

  • exists(p: function): boolean
  • Returns true if this option is nonempty and the given predicate returns true when applied on this option's value.

    Parameters

    • p: function

      is the predicate function to test

        • (a: A): boolean
        • Parameters

          • a: A

          Returns boolean

    Returns boolean

filter

  • filter<B>(p: function): Option<B>
  • filter(p: function): Option<A>
  • Returns this option if it is nonempty AND applying the predicate p to the underlying value yields true, otherwise return an empty option.

    Type parameters

    • B: A

    Parameters

    • p: function

      is the predicate function that is used to apply filtering on the option's value

        • (a: A): boolean
        • Parameters

          • a: A

          Returns boolean

    Returns Option<B>

    a new option instance containing the value of the source filtered with the given predicate

  • Parameters

    • p: function
        • (a: A): boolean
        • Parameters

          • a: A

          Returns boolean

    Returns Option<A>

flatMap

  • flatMap<B>(f: function): Option<B>
  • Returns the result of applying f to this option's value if the option is nonempty, otherwise returns an empty option.

    NOTE: this is similar with map, except that flatMap the result returned by f is expected to be boxed in an Option already.

    Example:

    const opt = Option.of(10)
    
    opt.flatMap(num => {
      if (num % 2 == 0)
        Some(num + 1)
      else
        None
    })
    

    Type parameters

    • B

    Parameters

    • f: function

      the mapping function that will transform the value of this option if nonempty.

    Returns Option<B>

    a new option instance containing the value of the source mapped by the given function

fold

  • fold<B>(fallback: function, f: function): B
  • Returns the result of applying f to this option's value, or in case the option is empty, the return the result of evaluating the fallback function.

    This function is equivalent with:

    opt.map(f).getOrElseL(fallback)
    

    Type parameters

    • B

    Parameters

    • fallback: function

      is the function to be evaluated in case this option is empty

        • (): B
        • Returns B

    • f: function

      is the mapping function for transforming this option's value in case it is nonempty

        • (a: A): B
        • Parameters

          • a: A

          Returns B

    Returns B

forAll

  • forAll(p: function): boolean
  • Returns true if this option is empty or the given predicate returns true when applied on this option's value.

    Parameters

    • p: function

      is the predicate function to test

        • (a: A): boolean
        • Parameters

          • a: A

          Returns boolean

    Returns boolean

forEach

  • forEach(cb: function): void
  • Apply the given procedure cb to the option's value if this option is nonempty, otherwise do nothing.

    Parameters

    • cb: function

      the procedure to apply

        • (a: A): void
        • Parameters

          • a: A

          Returns void

    Returns void

get

  • get(): A
  • Returns the option's value.

    WARNING!

    This function is partial, the option must be non-empty, otherwise a runtime exception will get thrown. Use with care.

    throws

    NoSuchElementError in case the option is empty

    Returns A

getOrElse

  • getOrElse<AA>(fallback: AA): A | AA
  • Returns the option's value if the option is nonempty, otherwise return the given fallback.

    See Option.getOrElseL for a lazy alternative.

    Type parameters

    • AA

    Parameters

    • fallback: AA

    Returns A | AA

getOrElseL

  • getOrElseL<AA>(thunk: function): A | AA
  • Returns the option's value if the option is nonempty, otherwise return the result of evaluating thunk.

    See Option.getOrElse for a strict alternative.

    Type parameters

    • AA

    Parameters

    • thunk: function
        • (): AA
        • Returns AA

    Returns A | AA

hashCode

  • hashCode(): number

isEmpty

  • isEmpty(): boolean
  • Returns true if the option is empty, false otherwise.

    Returns boolean

map

  • map<B>(f: function): Option<B>
  • Returns an option containing the result of applying f to this option's value, or an empty option if the source is empty.

    NOTE: this is similar with flatMap, except with map the result of f doesn't need to be wrapped in an Option.

    Type parameters

    • B

    Parameters

    • f: function

      the mapping function that will transform the value of this option if nonempty.

        • (a: A): B
        • Parameters

          • a: A

          Returns B

    Returns Option<B>

    a new option instance containing the value of the source mapped by the given function

nonEmpty

  • nonEmpty(): boolean
  • Returns true if the option is not empty, false otherwise.

    Returns boolean

orElse

  • Returns this option if it is nonempty, otherwise returns the given fallback.

    Type parameters

    • AA

    Parameters

    Returns Option<A | AA>

orElseL

  • orElseL<AA>(thunk: function): Option<A | AA>
  • Returns this option if it is nonempty, otherwise returns the given result of evaluating the given thunk.

    Type parameters

    • AA

    Parameters

    • thunk: function

      a no-params function that gets evaluated and whose result is returned in case this option is empty

    Returns Option<A | AA>

orNull

  • orNull(): A | null
  • Returns the option's value if the option is nonempty, otherwise return null. `

    Returns A | null

orUndefined

  • orUndefined(): A | undefined
  • Returns the option's value if the option is nonempty, otherwise return undefined.

    Returns A | undefined

Static empty

  • Returns an empty Option.

    Similar to Option.none, but this one allows specifying a type parameter (in the context of TypeScript or Flow or other type system).

    NOTE: Because Option is immutable, this function returns the same cached reference is on different calls.

    Type parameters

    • A

    Returns Option<A>

Static map2

  • Maps 2 optional values by the mapping function, returning a new optional reference that is Some only if both option values are Some, otherwise it returns a None.

    // Yields Some(3)
    Option.map2(Some(1), Some(2),
      (a, b) => a + b
    )
    
    // Yields None, because the second arg is None
    Option.map2(Some(1), None,
      (a, b) => a + b
    )
    

    This operation is the Applicative.map2.

    Type parameters

    • A1

    • A2

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • f: function
        • (a1: A1, a2: A2): R
        • Parameters

          • a1: A1
          • a2: A2

          Returns R

    Returns Option<R>

Static map3

  • Maps 3 optional values by the mapping function, returning a new optional reference that is Some only if all 3 option values are Some, otherwise it returns a None.

    // Yields Some(6)
    Option.map3(Some(1), Some(2), Some(3),
      (a, b, c) => a + b + c
    )
    
    // Yields None, because the second arg is None
    Option.map3(Some(1), None, Some(3),
      (a, b, c) => a + b + c
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • f: function
        • (a1: A1, a2: A2, a3: A3): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3

          Returns R

    Returns Option<R>

Static map4

  • Maps 4 optional values by the mapping function, returning a new optional reference that is Some only if all 4 option values are Some, otherwise it returns a None.

    // Yields Some(10)
    Option.map4(Some(1), Some(2), Some(3), Some(4),
      (a, b, c, d) => a + b + c + d
    )
    
    // Yields None, because the second arg is None
    Option.map4(Some(1), None, Some(3), Some(4),
      (a, b, c, d) => a + b + c + d
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4

          Returns R

    Returns Option<R>

Static map5

  • Maps 5 optional values by the mapping function, returning a new optional reference that is Some only if all 5 option values are Some, otherwise it returns a None.

    // Yields Some(15)
    Option.map5(Some(1), Some(2), Some(3), Some(4), Some(5),
      (a, b, c, d, e) => a + b + c + d + e
    )
    
    // Yields None, because the second arg is None
    Option.map5(Some(1), None, Some(3), Some(4), Some(5),
      (a, b, c, d, e) => a + b + c + d + e
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • A5

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • fa5: Option<A5>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4
          • a5: A5

          Returns R

    Returns Option<R>

Static map6

  • Maps 6 optional values by the mapping function, returning a new optional reference that is Some only if all 6 option values are Some, otherwise it returns a None.

    // Yields Some(21)
    Option.map6(Some(1), Some(2), Some(3), Some(4), Some(5), Some(6),
      (a, b, c, d, e, f) => a + b + c + d + e + f
    )
    
    // Yields None, because the second arg is None
    Option.map6(Some(1), None, Some(3), Some(4), Some(5), Some(6),
      (a, b, c, d, e, f) => a + b + c + d + e + f
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • A5

    • A6

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • fa5: Option<A5>
    • fa6: Option<A6>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4
          • a5: A5
          • a6: A6

          Returns R

    Returns Option<R>

Static none

  • Returns an empty Option.

    NOTE: Because Option is immutable, this function returns the same cached reference is on different calls.

    Type parameters

    • A

    Returns Option<A>

Static of

  • of<A>(value: A | null | undefined): Option<A>
  • Builds an Option reference that contains the given value.

    If the given value is null or undefined then the returned option will be empty.

    Type parameters

    • A

    Parameters

    • value: A | null | undefined

    Returns Option<A>

Static pure

  • pure<A>(value: A): Option<A>

Static some

  • some<A>(value: A): Option<A>
  • Builds an Option reference that contains the given reference.

    Note that value is allowed to be null or undefined, the returned option will still be non-empty. Use Option.of if you want to avoid this problem. This means:

    const opt = Some<number | null>(null)
    
    opt.isEmpty()
    //=> false
    
    opt.get()
    //=> null
    

    Type parameters

    • A

    Parameters

    • value: A

    Returns Option<A>

Static tailRecM

  • tailRecM<A, B>(a: A, f: function): Option<B>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc