Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Ok<T, E>

An Ok instance is the successful variant instance of the Result type, representing a successful outcome from an operation which may fail. For a full discussion, see the module docs.

Type parameters

  • T

    The type wrapped in this Ok variant of Result.

  • E

    The type which would be wrapped in an Err variant of Result.

Hierarchy

  • Ok

Implements

Index

Constructors

constructor

  • new Ok(value?: T | null): Ok
  • Create an instance of Result.Ok with new.

    Note: While you may create the Result type via normal JavaScript class construction, it is not recommended for the functional style for which the library is intended. Instead, use Result.ok.

    // Avoid:
    const aString = new Result.Ok('characters');
    
    // Prefer:
    const aString = Result.ok('characters);

    Note that you may explicitly pass Unit to the Ok constructor to create a Result<Unit, E>. However, you may not call the Ok constructor with null or undefined to get that result (the type system won't allow you to construct it that way). Instead, for convenience, you can simply call Result.ok(), which will construct the type correctly.

    throws

    If you pass null.

    Parameters

    • Optional value: T | null

      The value to wrap in a Result.Ok.

      Note: null and undefined are allowed by the type signature so that the constructor may throw on those rather than constructing a type like Result<undefined>.

    Returns Ok

Properties

value

value: T

The wrapped value.

variant

variant: Ok = Variant.Ok

Ok is always Variant.Ok.

Methods

and

andThen

  • andThen<U>(this: Result<T, E>, andThenFn: function): Result<U, E>

ap

chain

  • chain<U>(this: Result<T, E>, chainFn: function): Result<U, E>

equals

  • equals(this: Result<T, E>, comparison: Result<T, E>): boolean

flatMap

  • flatMap<U>(this: Result<T, E>, flatMapFn: function): Result<U, E>

isErr

  • isErr(this: Result<T, E>): this is Err<T, E>

isOk

  • isOk(this: Result<T, E>): this is Ok<T, E>

map

  • Method variant for Result.map

    Type parameters

    • U

    Parameters

    • this: Result<T, E>
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Result<U, E>

mapErr

  • mapErr<F>(this: Result<T, E>, mapErrFn: function): Result<T, F>

mapOr

  • mapOr<U>(this: Result<T, E>, orU: U, mapFn: function): U
  • Method variant for Result.mapOr

    Type parameters

    • U

    Parameters

    • this: Result<T, E>
    • orU: U
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

mapOrElse

  • mapOrElse<U>(this: Result<T, E>, orElseFn: function, mapFn: function): U
  • Method variant for Result.mapOrElse

    Type parameters

    • U

    Parameters

    • this: Result<T, E>
    • orElseFn: function
        • (err: E): U
        • Parameters

          • err: E

          Returns U

    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

match

or

orElse

  • orElse<F>(this: Result<T, E>, orElseFn: function): Result<T, F>

toJSON

toMaybe

toString

  • toString(this: Result<T, E>): string

unsafelyUnwrap

  • unsafelyUnwrap(): T

unsafelyUnwrapErr

  • unsafelyUnwrapErr(): never

unwrapOr

  • unwrapOr(this: Result<T, E>, defaultValue: T): T

unwrapOrElse

  • unwrapOrElse(this: Result<T, E>, elseFn: function): T

Static unwrap

  • unwrap<O>(theOk: Ok<O, any>): O
  • Unwrap the contained value. A convenience method for functional idioms.

    A common scenario where you might want to use this is in a pipeline of functions:

    import Result, { Ok } from 'true-myth/result';
    
    function getLengths(results: Array<Result<string, string>>): Array<number> {
    return results
    .filter(Result.isOk)
    .map(Ok.unwrap)
    .map(s => s.length);
    }

    Type parameters

    • O

    Parameters

    • theOk: Ok<O, any>

    Returns O

Generated using TypeDoc