ts-data-forge
    Preparing search index...
    • Unwraps a Result, returning the error value. Throws an error if the Result is Result.Ok.

      This function is used when you expect a Result to be an error and want to extract the error value. If the Result is unexpectedly Ok, it will throw an error with information about the unexpected success value.

      Type Parameters

      • R extends Result.Base

        The Result.Base type to unwrap.

      Parameters

      • result: R

        The Result to unwrap.

      • toStr: (v: UnwrapOk<R>) => string = toStr_

        An optional function to convert the success value to a string for the error message when the Result is unexpectedly Ok. Defaults to String.

      Returns UnwrapErr<R>

      The error value if Result.Err.

      const errResult = Result.err(new Error('broken'));
      const okResult = Result.ok('value');

      assert(Result.unwrapErrThrow(errResult).message === 'broken');
      assert.throws(() => Result.unwrapErrThrow(okResult), /Expected Err/u);

      Error with message "Expected Err but got Ok: {value}" if the Result is Result.Ok.