Options
All
  • Public
  • Public/Protected
  • All
Menu

@tsfun/result — Reference

Index

Type aliases

AsyncDeepErrorResult

AsyncDeepErrorResult<OuterPayload, Error, InnerPayload>: MaybePromise<Result<MaybePromise<OuterPayload>, AsyncResult<InnerPayload, Error>>>

Type parameters

  • OuterPayload

  • Error

  • InnerPayload

AsyncDeepPayloadResult

AsyncDeepPayloadResult<Payload, InnerError, OuterError>: MaybePromise<Result<AsyncResult<Payload, InnerError>, MaybePromise<OuterError>>>

Type parameters

  • Payload

  • InnerError

  • OuterError

AsyncResult

AsyncResult<Payload, Error>: MaybePromise<Result<MaybePromise<Payload>, MaybePromise<Error>>>

Type parameters

  • Payload

  • Error

DeepErrorResult

DeepErrorResult<OuterPayload, Error, InnerPayload>: Result<OuterPayload, Result<InnerPayload, Error>>

Type parameters

  • OuterPayload

  • Error

  • InnerPayload

DeepPayloadResult

DeepPayloadResult<Payload, InnerError, OuterError>: Result<Result<Payload, InnerError>, OuterError>

Type parameters

  • Payload

  • InnerError

  • OuterError

Functions

Const and

  • and<Left, Right, Error>(left: Result<Left, Error>, right: Result<Right, Error>): any
  • Return err(error) if left is err(error), otherwise return right

    Type parameters

    • Left = never

    • Right = Left

    • Error = never

    Parameters

    • left: Result<Left, Error>

      Result to match against

    • right: Result<Right, Error>

      Result to return when left carries an error

    Returns any

    Either right or an Err

Const andThen

  • andThen<Left, Right, Error>(left: Result<Left, Error>, right: (payload: Left) => Result<Right, Error>): any
  • Return err(error) if left is err(error), and return ok(right(x)) if left is ok(x)

    Type parameters

    • Left = never

    • Right = Left

    • Error = never

    Parameters

    • left: Result<Left, Error>

      Result to match against

    • right: (payload: Left) => Result<Right, Error>

      Function to call when left carries an error

        • (payload: Left): Result<Right, Error>
        • Parameters

          • payload: Left

          Returns Result<Right, Error>

    Returns any

    Either return value of right or an Err

asyncFlatten

  • asyncFlatten<Payload, InnerError, OuterError>(deepResultPromise: AsyncDeepPayloadResult<Payload, InnerError, OuterError>): Promise<Result<Payload, InnerError | OuterError>>
  • Flatten (a promise of) a result that carries another (promise of a) result as a payload

    Type parameters

    • Payload = never

    • InnerError = never

    • OuterError = InnerError

    Parameters

    Returns Promise<Result<Payload, InnerError | OuterError>>

    Promise that resolves flattened result

asyncFlattenError

  • asyncFlattenError<OuterPayload, Error, InnerPayload>(deepResultPromise: AsyncDeepErrorResult<OuterPayload, Error, InnerPayload>): Promise<Result<OuterPayload | InnerPayload, Error>>
  • Flatten (a promise of) a result that carries another (promise of a) result as an error

    Type parameters

    • OuterPayload = never

    • Error = never

    • InnerPayload = OuterPayload

    Parameters

    Returns Promise<Result<OuterPayload | InnerPayload, Error>>

    Promise that resolves flattened result

Const asyncUnwrap

  • asyncUnwrap<Payload>(result: AsyncResult<Payload, any>): Promise<never>
  • Return a promise that resolves x if await result is ok(x) and rejects with reason if await result is err(reason)

    Type parameters

    • Payload = never

    Parameters

    Returns Promise<never>

    Promise that resolves carried payload

Const asyncUnwrapOr

  • asyncUnwrapOr<Payload, Default>(result: AsyncResult<Payload, any>, def: MaybePromise<Default>): Promise<any>
  • Return a promise that resolves x if await result is ok(x) and resolves def otherwise

    Type parameters

    • Payload = never

    • Default = Payload

    Parameters

    • result: AsyncResult<Payload, any>

      Result to unwrap

    • def: MaybePromise<Default>

      Value to resolve when await result carries an error

    Returns Promise<any>

    Promise that resolves either carried payload or def

Const asyncUnwrapOrElse

  • asyncUnwrapOrElse<Payload, Error, Default>(result: AsyncResult<Payload, Error>, def: (error: Error) => MaybePromise<Default>): Promise<any>
  • Return a promise that resolves x if await result is ok(x) and resolves def(await error) if await result is err(error)

    Type parameters

    • Payload = never

    • Error = never

    • Default = Payload

    Parameters

    • result: AsyncResult<Payload, Error>

      Result to unwrap

    • def: (error: Error) => MaybePromise<Default>

      Function to execute when await result carries an error

        • (error: Error): MaybePromise<Default>
        • Parameters

          • error: Error

          Returns MaybePromise<Default>

    Returns Promise<any>

    Promise that resolves either carried payload or return value of def

Const clone

  • clone<Value, Error>(result: Result<Value, Error>): never
  • Clone a result

    Type parameters

    • Value = never

    • Error = never

    Parameters

    • result: Result<Value, Error>

      Result to clone

    Returns never

    A new copy of result

Const expectSome

  • expectSome<Payload>(option: Option<Payload>): Result<Payload, Error>
  • If option is some(x), return ok(x), otherwise return an Err

    Type parameters

    • Payload = never

    Parameters

    • option: Option<Payload>

      Option to match against

    Returns Result<Payload, Error>

    Result

Const expectSomeOr

  • expectSomeOr<Payload, Error>(option: Option<Payload>, error: Error): Result<Payload, Error>
  • If option is some(x), return ok(x), otherwise return err(error)

    Type parameters

    • Payload = never

    • Error = never

    Parameters

    • option: Option<Payload>

      Option to match against

    • error: Error

      Error to carry should option is a None

    Returns Result<Payload, Error>

    Result

Const expectSomeOrElse

  • expectSomeOrElse<Payload, Error>(option: Option<Payload>, error: () => Error): Result<Payload, Error>
  • If option is some(x), return ok(x), otherwise return err(error())

    Type parameters

    • Payload = never

    • Error = never

    Parameters

    • option: Option<Payload>

      Result to match against

    • error: () => Error

      Function to execute should option is a None

        • (): Error
        • Returns Error

    Returns Result<Payload, Error>

    Result

Const flatten

  • flatten<Payload, InnerError, OuterError>(deepResult: DeepPayloadResult<Payload, InnerError, OuterError>): Result<Payload, InnerError | OuterError>
  • Return a result that is carried as a payload by deepResult

    Type parameters

    • Payload = never

    • InnerError = never

    • OuterError = InnerError

    Parameters

    Returns Result<Payload, InnerError | OuterError>

    Result with a flattened payload

Const flattenError

  • flattenError<OuterPayload, Error, InnerPayload>(deepResult: DeepErrorResult<OuterPayload, Error, InnerPayload>): Result<OuterPayload | InnerPayload, Error>
  • Return a result that is carried as an error by deepResult

    Type parameters

    • OuterPayload = never

    • Error = never

    • InnerPayload = OuterPayload

    Parameters

    • deepResult: DeepErrorResult<OuterPayload, Error, InnerPayload>

      Result to flatten

    Returns Result<OuterPayload | InnerPayload, Error>

    Result with a flattened error

Const flip

  • flip<A, B>(result: Result<A, B>): Result<B, A>
  • Return err(x) if result is ok(x) and return ok(x) if result is err(x)

    Type parameters

    • A = never

    • B = never

    Parameters

    • result: Result<A, B>

      Result to flip

    Returns Result<B, A>

    Flipped result

Const map

  • map<Payload, Error, Return>(result: Result<Payload, Error>, fn: (payload: Payload) => Return): Result<Return, Error>
  • Apply a function to the carried payload (if any)

    Type parameters

    • Payload = never

    • Error = never

    • Return = Payload

    Parameters

    • result: Result<Payload, Error>

      Result to match against

    • fn: (payload: Payload) => Return

      Function to call when result carries a payload

        • (payload: Payload): Return
        • Parameters

          • payload: Payload

          Returns Return

    Returns Result<Return, Error>

    Result that may carry fn's return value as a payload

Const mapErr

  • mapErr<Error, Payload, Return>(result: Result<Payload, Error>, fn: (error: Error) => Return): Result<Payload, Return>
  • Apply a function to the carried error (if any)

    Type parameters

    • Error = never

    • Payload = never

    • Return = Error

    Parameters

    • result: Result<Payload, Error>

      Result to match against

    • fn: (error: Error) => Return

      Function to call when result carries an error

        • (error: Error): Return
        • Parameters

          • error: Error

          Returns Return

    Returns Result<Payload, Return>

    Result that may carry fn's return value as an error

Const mapOrElse

  • mapOrElse<Payload, Error, OkReturn, ErrReturn>(result: Result<Payload, Error>, handleErr: (error: Error) => ErrReturn, handleOk: (payload: Payload) => OkReturn): Result<OkReturn, ErrReturn>
  • Apply handleErr to a carried error or handleOk to a carried payload

    Type parameters

    • Payload = never

    • Error = never

    • OkReturn = Payload

    • ErrReturn = Error

    Parameters

    • result: Result<Payload, Error>

      Result to match against

    • handleErr: (error: Error) => ErrReturn

      Function to call when result carries an error

        • (error: Error): ErrReturn
        • Parameters

          • error: Error

          Returns ErrReturn

    • handleOk: (payload: Payload) => OkReturn

      Function to call when result carries a payload

        • (payload: Payload): OkReturn
        • Parameters

          • payload: Payload

          Returns OkReturn

    Returns Result<OkReturn, ErrReturn>

    Result that may carry handleErr's return value as an error or handleOk's as a payload

Const match

  • match<Payload, Error, OkReturn, ErrReturn>(result: Result<Payload, Error>, handle: MatchHandlers<Payload, Error, OkReturn, ErrReturn>): OkReturn | ErrReturn
  • Match a result against a pair of functions

    Type parameters

    • Payload = never

    • Error = never

    • OkReturn = Payload

    • ErrReturn = Error

    Parameters

    • result: Result<Payload, Error>

      Result to match against

    • handle: MatchHandlers<Payload, Error, OkReturn, ErrReturn>

      Functions to handle each case

    Returns OkReturn | ErrReturn

    Return value of either function

Const or

  • or<Payload, Left, Right>(left: Result<Payload, Left>, right: Result<Payload, Right>): any
  • Return left if it carries a payload, otherwise return right

    Type parameters

    • Payload = never

    • Left = never

    • Right = Left

    Parameters

    • left: Result<Payload, Left>

      Result to match against

    • right: Result<Payload, Right>

      Result to return when left carries an error

    Returns any

    Either left or right

Const orElse

  • orElse<Payload, Left, Right>(left: Result<Payload, Left>, right: (error: Left) => Result<Payload, Right>): any
  • Return left if it carries a payload, otherwise execute right with error of left and return the result

    Type parameters

    • Payload = never

    • Left = never

    • Right = Left

    Parameters

    • left: Result<Payload, Left>

      Result to match against

    • right: (error: Left) => Result<Payload, Right>

      Function to call when left carries an error

        • (error: Left): Result<Payload, Right>
        • Parameters

          • error: Left

          Returns Result<Payload, Right>

    Returns any

    Either left or return value of right

transpose

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

    Type parameters

    • Payload = never

    • Error = never

    Parameters

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

      Result of Option

    Returns Option<Result<Payload, Error>>

    Option of Result

tryExec

  • tryExec<Args, Return>(fn: (...args: Args) => Return, ...args: Args): Result<Return, any>
  • Return ok(x) if executing fn returns x, and return err(error) if executing fn throws error

    Type parameters

    • Args: any[]

    • Return

    Parameters

    • fn: (...args: Args) => Return

      Function to execute

        • (...args: Args): Return
        • Parameters

          • Rest ...args: Args

          Returns Return

    • Rest ...args: Args

      Arguments to pass to function

    Returns Result<Return, any>

    Result of function execution

Const unwrap

  • unwrap<Payload>(result: Result<Payload, any>): never
  • Return x if result is ok(x), and throw error if result is err(error)

    Type parameters

    • Payload = never

    Parameters

    • result: Result<Payload, any>

      Result to unwrap

    Returns never

    Carried payload

Const unwrapOr

  • unwrapOr<Payload, Default>(result: Result<Payload, any>, def: Default): Default
  • Return x if result is ok(x), otherwise return def

    Type parameters

    • Payload = never

    • Default = Payload

    Parameters

    • result: Result<Payload, any>

      Result to unwrap

    • def: Default

      Value to return when result carries an error

    Returns Default

    Either carried payload or def

Const unwrapOrElse

  • unwrapOrElse<Payload, Error, Default>(result: Result<Payload, Error>, def: (error: Error) => Default): Default
  • Return x if result is ok(x), and return def(error) if result is err(error)

    Type parameters

    • Payload = never

    • Error = never

    • Default = Payload

    Parameters

    • result: Result<Payload, Error>

      Result to unwrap

    • def: (error: Error) => Default

      Function to call when result carries an error

        • (error: Error): Default
        • Parameters

          • error: Error

          Returns Default

    Returns Default

    Either carried payload of result or returning value of def

Generated using TypeDoc