Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Result<T, E>

Manages access to a value that represents either success (Ok) or failure (Err).

let result: Result<string, number>;

// A value for success.
result = ok('success');

// A value for failure.
result = err(123);

Type parameters

  • T

    The type of the success value.

  • E

    The type of the failure value.

Hierarchy

  • Result

Index

Methods

and

  • Returns the other result if this result is Ok.

    let result = ok('example');
    let other = ok('other');
    let output = result.and(other);
    
    assert(output === other);
    

    If this result is Err, then this result is returned.

    result = err(123);
    output = result.and(other);
    
    assert(output === result);
    

    Type parameters

    • U

      The type of the value in the other result.

    Parameters

    • other: Result<U, E>

      The other result.

    Returns Result<U, E>

    The other result or this Err.

andThen

  • Returns the computed other result if this result is Ok.

    let result = ok('example');
    let fn = r => ok(r + ' other');
    let output = result.andThen(fn);
    
    assert(output.unwrap() === 'example other');
    

    If this result is Err, then this result is returned.

    result = err(123);
    output = result.andThen(fn);
    
    assert(output === result);
    

    Type parameters

    • U

      The type of the value in the computed result.

    Parameters

    Returns Result<U, E>

    The other result or this Err.

err

  • Transforms this Result<T, E> into Some<E> if it is Err.

    let result = err(123);
    let output = result.err();
    
    assert(output.isSome() === true);
    assert(output.unwrap() === 123);
    

    If this result is Ok, then None<E> is returned.

    result = ok('example');
    output = result.err();
    
    assert(output.isNone());
    

    Returns Option<E>

    The option.

expect

  • expect(message: string): T
  • Returns the value in this result if Ok.

    let message = 'Example error message.';
    let result = ok('example');
    let output = result.expect(message);
    
    assert(output === 'example');
    

    If this result is Err, an error is thrown.

    result = err(123);
    output = result.expect(message);
    
    // throws error
    
    throws

    {ResultError} If this is Err.

    Parameters

    • message: string

      The error message.

    Returns T

    The value.

expectErr

  • expectErr(message: string): E
  • Returns the value in this result if Err.

    let message = 'Example error message.';
    let result = err(123);
    let output = result.expectErr(message);
    
    assert(output === 123);
    

    If this result is Ok, an error is thrown.

    result = ok('example');
    output = result.expectErr(message);
    
    // throws error
    
    throws

    {ResultError} If this is Ok.

    Parameters

    • message: string

      The error message.

    Returns E

    The value.

isErr

  • isErr(): boolean
  • Returns true if this result is Error.

    let result = err(123);
    let output = result.isErr();
    
    assert(output === true);
    

    If this result is Ok, then false is returned.

    result = ok('example');
    output = result.isErr();
    
    assert(output === false);
    

    Returns boolean

    Returns true if Err, or false if not.

isOk

  • isOk(): boolean
  • Returns true if this result is Ok.

    let result = ok('example');
    let output = result.isOk();
    
    assert(output === true);
    

    If this result is Err, then false is returned.

    result = err(123);
    output = result.isOk();
    
    assert(output === false);
    

    Returns boolean

    Returns true if Ok, or false if not.

map

  • Returns the mapped result if this result is `Ok.

    let fn = r => r.length;
    let result = ok('example');
    let output = result.map(fn);
    
    assert(output.isOk() === true);
    assert(output.unwrap() === 7);
    

    If this result is Err, this result is returned.

    result = err(123);
    output = result.map(fn);
    
    assert(output === result);
    

    Type parameters

    • U

      The type of the value in the computed result.

    Parameters

    • fn: Compute<T, U>

      The mapping function.

    Returns Result<U, E>

    The result.

mapErr

  • Returns the mapped result if this result is Err.

    let fn = r => r + 111;
    let result = err(123);
    let output = result.mapErr(fn);
    
    assert(output.isErr() === true);
    assert(output.unwrap() === 456);
    

    If this result is Ok, this result is returned.

    result = ok('example');
    output = result.mapErr(fn);
    
    assert(output === result);
    

    Type parameters

    • F

      The type of the error in the computed result.

    Parameters

    • fn: Compute<E, F>

      The mapping function.

    Returns Result<T, F>

    The mapped result or this result.

mapOr

  • mapOr<U>(def: U, fn: Compute<T, U>): U
  • Returns the mapped value if this result is Ok.

    let def = 456;
    let fn = r => r.length;
    let result = ok('example');
    let output = result.mapOr(def, fn);
    
    assert(output === 7);
    

    If this result is Err, then the default value is returned.

    result = err(123);
    output = result.mapOr(def, fn);
    
    assert(output === 456);
    

    Type parameters

    • U

      The type of the computed or default value.

    Parameters

    • def: U

      The default value.

    • fn: Compute<T, U>

      The mapping function.

    Returns U

    The mapped or default value.

mapOrElse

  • Returns the mapped value if this result is Ok.

    let def = r => r + 333;
    let fn = r => r.length;
    let result = ok('example');
    let output = result.mapOrElse(def, fn);
    
    assert(output === 7);
    

    If this result is Err, then the default value is computed and returned.

    result = err(123);
    output = result.mapOrElse(def, fn);
    
    assert(output === 456);
    

    Type parameters

    • U

      The type of the computed or default value.

    Parameters

    • def: Compute<E, U>

      The default value computer.

    • fn: Compute<T, U>

      The mapping function.

    Returns U

    The mapped or computed default value.

ok

  • Converts this result to Some<T> if this result is Ok.

    let result = ok('example');
    let output = result.ok();
    
    assert(output.isSome() === true);
    assert(output.unwrap() === 'example');
    

    If this result is Err, then None<T> is returned.

    result = err(123);
    output = result.ok();
    
    assert(output.isNone() === true);
    

    Returns Option<T>

    Returns Some if Ok, or None if Error.

or

  • Returns this result if this result is Ok.

    let other = ok('other');
    let result = ok('example');
    let output = result.or(other);
    
    assert(output === result);
    

    If this result is Err, then the other result is returned.

    result = err(123);
    output = result.or(other);
    
    assert(output === other);
    

    If both this and the other result is Err, the other result is returned.

    other = err(456);
    result = err(123);
    output = result.or(other);
    
    assert(output === other);
    

    Type parameters

    • F

      The type of the error in the computed result.

    Parameters

    • other: Result<T, F>

      The other result.

    Returns Result<T, F>

    This or the other result.

orElse

  • Returns this result if this result is Ok.

    let fn = () => ok('default');
    let result = ok('example');
    let output = result.orElse(fn);
    
    assert(output === result);
    

    If this result is Err, then the default result is computed and returned.

    result = err(123);
    output = result.orElse(fn);
    
    assert(output === 'default');
    

    Type parameters

    • F

      The type of the error in the computed result.

    Parameters

    Returns Result<T, F>

    This or the default result.

unwrap

  • unwrap(): T
  • Returns the value in this result if this result is Ok.

    let result = ok('example');
    let output = result.unwrap();
    
    assert(output === 'example');
    

    If this result is Err, an error is thrown.

    result = err(123);
    output = result.unwrap();
    
    // throws error
    
    throws

    {ResultError} If this result is not Ok.

    Returns T

    The value.

unwrapErr

  • unwrapErr(): E
  • Returns the value in this result if this result is Err.

    let result = err(123);
    let output = result.unwrapErr();
    
    assert(output === 123);
    

    If this result is Ok, an error is thrown.

    result = ok('example');
    output = result.unwrapErr();
    
    // throws error
    
    throws

    {ResultError} If this result is not Err.

    Returns E

    The value.

unwrapOr

  • unwrapOr(def: T): T
  • Returns the value in this result if this result is Ok.

    let def = 'default';
    let result = ok('example');
    let output = result.unwrapOr(default);
    
    assert(output === 'example');
    

    If this result is Err, then the default value is returned.

    result = err(123);
    output = result.unwrapOr(default);
    
    assert(output === def);
    

    Parameters

    • def: T

      The default value.

    Returns T

    The value in this result or the default value.

unwrapOrElse

  • unwrapOrElse(fn: Compute<E, T>): T
  • Returns the value in this result if this result is Ok.

    let fn = () => 'default';
    let result = ok('example');
    let output = result.unwrapOrElse(fn);
    
    assert(output === 'example');
    

    If this result is Err, then the default value is computed and returned.

    result = err(123);
    output = result.unwrapOrElse(fn);
    
    assert(output === 'default');
    

    Parameters

    • fn: Compute<E, T>

      The default value computer.

    Returns T

    The value in this result or the default value.

Generated using TypeDoc