Options
All
  • Public
  • Public/Protected
  • All
Menu

@tsfun/option — Reference

Index

Type aliases

AsyncDeepOption

AsyncDeepOption<Value>: MaybePromise<Option<AsyncOption<Value>>>

Type parameters

  • Value

AsyncOption

AsyncOption<Value>: MaybePromise<Option<MaybePromise<Value>>>

Type parameters

  • Value

DeepOption

DeepOption<Value>: Option<Option<Value>>

Type parameters

  • Value

MakeReturn

MakeReturn<Value>: Value extends null ? None : Value extends undefined ? None : null extends Value ? Option<Value> : undefined extends Value ? Option<Value> : Some<Value>

Type parameters

  • Value

Functions

Const and

  • and<Left, Right>(left: Option<Left>, right: Option<Right>): any
  • Return a None if left does not contain a value, otherwise return right

    Type parameters

    • Left = never

    • Right = Left

    Parameters

    • left: Option<Left>

      Option to match against

    • right: Option<Right>

      Option to return when left contains a value

    Returns any

    Either right or a None

Const andThen

  • andThen<Left, Right>(left: Option<Left>, right: (value: Left) => Option<Right>): any
  • Return None if left does not contain a value, otherwise call right with the wrapped value and return the result

    Type parameters

    • Left = never

    • Right = Left

    Parameters

    • left: Option<Left>

      Option to match against

    • right: (value: Left) => Option<Right>

      Function to call when left contains a value

        • (value: Left): Option<Right>
        • Parameters

          • value: Left

          Returns Option<Right>

    Returns any

    Either returning value of right or a None

asyncFlatten

  • asyncFlatten<Value>(deepOptionPromise: AsyncDeepOption<Value>): Promise<Option<Value>>
  • Flatten deepOptionPromise

    Type parameters

    • Value = never

    Parameters

    Returns Promise<Option<Value>>

    Promise that resolves flattened option

Const asyncUnwrap

  • asyncUnwrap<Value>(option: AsyncOption<Value>): Promise<never>
  • Turn a Some into a Promise that resolves or turn a None into a Promise that rejects

    Type parameters

    • Value = never

    Parameters

    Returns Promise<never>

    A Promise that may resolve contained value

Const asyncUnwrapOr

  • asyncUnwrapOr<Value, Default>(option: AsyncOption<Value>, def: Default): Promise<Default>
  • Return contained value of a Some or default value

    Type parameters

    • Value = never

    • Default = Value

    Parameters

    • option: AsyncOption<Value>

      Option to unwrap

    • def: Default

      Value to return when option does not contain a value

    Returns Promise<Default>

    Promise that resolves contained value or def

Const asyncUnwrapOrElse

  • asyncUnwrapOrElse<Value, Default>(option: AsyncOption<Value>, def: () => Default | Promise<Default>): Promise<Default>
  • Return contained value of a Some or call a function

    Type parameters

    • Value = never

    • Default = Value

    Parameters

    • option: AsyncOption<Value>

      Option to unwrap

    • def: () => Default | Promise<Default>

      Function to be called when option does not contain a value

        • (): Default | Promise<Default>
        • Returns Default | Promise<Default>

    Returns Promise<Default>

    Promise that resolves to either contained value or returning value of none

Const asyncUnwrapOrErr

  • asyncUnwrapOrErr<Value, Message>(option: AsyncOption<Value>, message: Message): Promise<never>
  • Unwrap contained value of a Some, or throws an error

    Type parameters

    • Value = never

    • Message = Error

    Parameters

    • option: AsyncOption<Value>

      Option to unwrap

    • message: Message

      Error to throw when option does not contain a value

    Returns Promise<never>

    Contained value

Const clone

  • clone<Value>(option: Option<Value>): never
  • Clone an option

    Type parameters

    • Value = never

    Parameters

    • option: Option<Value>

      Option to clone

    Returns never

    A new copy of option

Const expectOk

  • expectOk<Payload>(result: Result<Payload, any>): Option<Payload>
  • Convert ok(x) to an some(x) and convert err(_) to none()

    Type parameters

    • Payload = never

    Parameters

    • result: Result<Payload, any>

      Result to convert

    Returns Option<Payload>

    Corresponding Option

    In reality, this function merely returns the same object it takes since Result interface is compatible with Option

Const filter

  • filter<Value>(option: Option<Value>, predicate: (value: Value) => boolean): any
  • Return a None if option does not contain a value, otherwise call predicate and return:

    • a None if predicate returns false
    • a Some if predicate returns true

    Type parameters

    • Value = never

    Parameters

    • option: Option<Value>

      Option to match against

    • predicate: (value: Value) => boolean

      Whether should filter return a Some when option contains a value

        • (value: Value): boolean
        • Parameters

          • value: Value

          Returns boolean

    Returns any

    An Option

Const flatten

  • flatten<Value>(deepOption: DeepOption<Value>): any
  • Return contained option within deepOption

    Type parameters

    • Value = never

    Parameters

    Returns any

    Flattened option

iter

  • iter<Value>(option: Option<Value>): Generator<any, void, unknown>
  • Return an iterator over the possibly contained value

    Type parameters

    • Value = never

    Parameters

    • option: Option<Value>

      Option to iterate over

    Returns Generator<any, void, unknown>

    An iterator that yields contained value

Const map

  • map<Value, Return>(option: Option<Value>, fn: (value: Value) => Return): Option<Return>
  • Apply a function to the contained value (if any)

    Type parameters

    • Value = never

    • Return = Value

    Parameters

    • option: Option<Value>

      Option to match against

    • fn: (value: Value) => Return

      Function to call when option contains a value

        • (value: Value): Return
        • Parameters

          • value: Value

          Returns Return

    Returns Option<Return>

    An Option of fn's return value

Const match

  • match<Value, SomeReturn, NoneReturn>(option: Option<Value>, handle: MatchHandlers<Value, SomeReturn, NoneReturn>): SomeReturn | NoneReturn
  • Match an option against a pair of functions

    Type parameters

    • Value = never

    • SomeReturn = Value

    • NoneReturn = SomeReturn

    Parameters

    • option: Option<Value>

      Option to match against

    • handle: MatchHandlers<Value, SomeReturn, NoneReturn>

      Functions to handle each case

    Returns SomeReturn | NoneReturn

Const option

  • Turn null or undefined into a None and others into a Some

    Type parameters

    • Value

    Parameters

    • value: Value

      Nullable value

    Returns MakeReturn<Value>

    An Option

Const or

  • or<Value>(left: Option<Value>, right: Option<Value>): any
  • Return left if it contains a value, otherwise return right

    Type parameters

    • Value = never

    Parameters

    • left: Option<Value>

      Option to match against

    • right: Option<Value>

      Option to return when left does not contain a value

    Returns any

    Either left or right

Const orElse

  • orElse<Value>(left: Option<Value>, right: () => Option<Value>): any
  • Return left if it contains a value, otherwise call right and return the result

    Type parameters

    • Value = never

    Parameters

    • left: Option<Value>

      Option to match against

    • right: () => Option<Value>

      Function to call when left does not contain a value

        • (): Option<Value>
        • Returns Option<Value>

    Returns any

    Either left or return value of right

transpose

  • transpose<Payload, Error>(option: Option<Result<Payload, Error>>): Result<Option<Payload>, Error>
  • Transposes an Option of Result into a Result of Option

    Type parameters

    • Payload = never

    • Error = never

    Parameters

    • option: Option<Result<Payload, Error>>

      Option of Result

    Returns Result<Option<Payload>, Error>

    Result of Option

Const tuple

  • tuple<Value>(option: Option<Value>): [Value] | []
  • Return a tuple of the contained value if option contains a value, otherwise return an empty tuple

    Type parameters

    • Value = never

    Parameters

    • option: Option<Value>

      Option to get value from

    Returns [Value] | []

    An array that may contain contained value

Const unwrap

  • unwrap<Value>(option: Option<Value>): never
  • Return contained value of a Some or throw a TypeError

    Type parameters

    • Value = never

    Parameters

    • option: Option<Value>

      Option to unwrap

    Returns never

    Contained value

Const unwrapOr

  • unwrapOr<Value, Default>(option: Option<Value>, def: Default): Default
  • Return contained value of a Some or default value

    Type parameters

    • Value = never

    • Default = Value

    Parameters

    • option: Option<Value>

      Option to unwrap

    • def: Default

      Value to return when option does not contain a value

    Returns Default

    Either contained value or def

Const unwrapOrElse

  • unwrapOrElse<Value, Default>(option: Option<Value>, def: () => Default): Default
  • Return contained value of a Some or call a function

    Type parameters

    • Value = never

    • Default = Value

    Parameters

    • option: Option<Value>

      Option to unwrap

    • def: () => Default

      Function to be called when option does not contain a value

        • (): Default
        • Returns Default

    Returns Default

    Either contained value or returning value of none

Const unwrapOrErr

  • unwrapOrErr<Value, Message>(option: Option<Value>, message: Message): never
  • Unwrap contained value of a Some, or throws an error

    Type parameters

    • Value = never

    • Message = Error

    Parameters

    • option: Option<Value>

      Option to unwrap

    • message: Message

      Error to throw when option does not contain a value

    Returns never

    Contained value

Generated using TypeDoc