diff --git a/src/result.ts b/src/result.ts index 6d5c9da..c660515 100644 --- a/src/result.ts +++ b/src/result.ts @@ -25,6 +25,12 @@ interface BaseResult extends Iterable ? U : ne */ expect(msg: string): T; + /** + * Returns the contained `Ok` value, if does not exist. Throws an error if it does. + * @param msg the message to throw if Ok value. + */ + expectErr(msg: string): T; + /** * Returns the contained `Ok` value. * Because this function may throw, its use is generally discouraged. @@ -136,6 +142,10 @@ export class ErrImpl implements BaseResult { throw new Error(`${msg} - Error: ${toString(this.val)}\n${this._stack}`); } + expectErr(_msg: string): E { + return this.val + } + unwrap(): never { throw new Error(`Tried to unwrap Error: ${toString(this.val)}\n${this._stack}`); } @@ -220,6 +230,10 @@ export class OkImpl implements BaseResult { return this.val; } + expectErr(msg: string): never { + throw new Error(msg); + } + unwrap(): T { return this.val; }