diff --git a/packages/result/src/lib/Result/Err.ts b/packages/result/src/lib/Result/Err.ts index 71f1dfe451..da935c1365 100644 --- a/packages/result/src/lib/Result/Err.ts +++ b/packages/result/src/lib/Result/Err.ts @@ -115,6 +115,11 @@ export class Err implements IResult { return op(this.error); } + public unwrapRaw(): never { + // eslint-disable-next-line @typescript-eslint/no-throw-literal + throw this.error; + } + public and(result?: Result): this; public and(): this { return this; diff --git a/packages/result/src/lib/Result/IResult.ts b/packages/result/src/lib/Result/IResult.ts index d03ce92fb8..0cca637037 100644 --- a/packages/result/src/lib/Result/IResult.ts +++ b/packages/result/src/lib/Result/IResult.ts @@ -449,6 +449,8 @@ export interface IResult { * If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`. * @seealso {@link unwrapOr} * @seealso {@link unwrapOrElse} + * @seealso {@link unwrapErr} + * @seealso {@link unwrapRaw} * * @example * ```typescript @@ -473,6 +475,10 @@ export interface IResult { * Returns the contained `Err` value. * * If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`. + * @seealso {@link unwrap} + * @seealso {@link unwrapOr} + * @seealso {@link unwrapOrElse} + * @seealso {@link unwrapRaw} * * @example * ```typescript @@ -498,6 +504,11 @@ export interface IResult { * * Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is * recommended to use {@link unwrapOrElse}, which is lazily evaluated. + * @seealso {@link unwrap} + * @seealso {@link unwrapOrElse} + * @seealso {@link unwrapErr} + * @seealso {@link unwrapRaw} + * * @param defaultValue The default value. * * @example @@ -517,6 +528,11 @@ export interface IResult { /** * Returns the contained `Ok` value or computes it from a closure. + * @seealso {@link unwrap} + * @seealso {@link unwrapOr} + * @seealso {@link unwrapErr} + * @seealso {@link unwrapRaw} + * * @param op The predicate. * * @example @@ -531,6 +547,32 @@ export interface IResult { */ unwrapOrElse(op: (error: E) => V): T | V; + /** + * Returns the contained `Ok` value. + * + * If the value is an `Err`, it throws the contained error. + * @seealso {@link unwrap} + * @seealso {@link unwrapOr} + * @seealso {@link unwrapOrElse} + * @seealso {@link unwrapErr} + * + * @example + * ```typescript + * const x = ok(2); + * assert.equal(x.unwrapRaw(), 2); + * ``` + * @example + * ```typescript + * const x = err('Emergency failure'); + * assert.throws(() => x.unwrapRaw(), { + * name: 'Error', + * message: 'Unwrap failed', + * value: 'Emergency failure' + * }); + * ``` + */ + unwrapRaw(): T | never; + /** * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself. * @param result The result to check. diff --git a/packages/result/src/lib/Result/Ok.ts b/packages/result/src/lib/Result/Ok.ts index 38c4210292..e7867ba457 100644 --- a/packages/result/src/lib/Result/Ok.ts +++ b/packages/result/src/lib/Result/Ok.ts @@ -116,6 +116,10 @@ export class Ok implements IResult { return this.value; } + public unwrapRaw(): T { + return this.value; + } + public and>(result: R): R { return result; } diff --git a/packages/result/tests/Result.test.ts b/packages/result/tests/Result.test.ts index 194fb45af5..5b7452b176 100644 --- a/packages/result/tests/Result.test.ts +++ b/packages/result/tests/Result.test.ts @@ -438,6 +438,21 @@ describe('Result', () => { }); }); + describe('unwrapRaw', () => { + test('GIVEN ok THEN returns value', () => { + const x = ok(2); + + expect(x.unwrapRaw()).toBe(2); + }); + + test('GIVEN err THEN throws Error', () => { + const error = new Error('Some error message'); + const x = err(error); + + expect(() => x.unwrapRaw()).toThrowError(error); + }); + }); + describe('and', () => { test('GIVEN x=ok and y=ok THEN returns y', () => { const x = ok(2);