diff --git a/benchmarks/tryblock.bench.ts b/benchmarks/tryblock.bench.ts index f9c2de4..e926ecc 100644 --- a/benchmarks/tryblock.bench.ts +++ b/benchmarks/tryblock.bench.ts @@ -14,7 +14,7 @@ const af = asyncFn(async () => { if (one.isErr()) { return one; } - return Ok(one.unwrap() + 1); + return Ok(one.unwrapUnchecked() + 1); }); function tba() { diff --git a/example/main.ts b/example/main.ts index faf95a1..7af1a03 100644 --- a/example/main.ts +++ b/example/main.ts @@ -68,11 +68,11 @@ const getAverageGrade = asyncFn(async (studentId: string) => { }); if (grades.isErr()) { - return Err(grades.unwrapErr()); + return Err(grades.unwrapErrUnchecked()); } // Safe to unwrap because we checked for error - const value = grades.unwrap(); + const value = grades.unwrapUnchecked(); return divide( value.reduce((a, b) => a + b, 0), value.length, diff --git a/src/async_result.ts b/src/async_result.ts index c62699b..9c63cc0 100644 --- a/src/async_result.ts +++ b/src/async_result.ts @@ -753,7 +753,7 @@ export class AsyncResult implements PromiseLike> { * ``` */ public async unwrap(): Promise { - return (await this).unwrap(); + return (await this).unwrapUnchecked(); } /** @@ -785,7 +785,7 @@ export class AsyncResult implements PromiseLike> { * ``` */ public async unwrapErr(): Promise { - return (await this).unwrapErr(); + return (await this).unwrapErrUnchecked(); } /** diff --git a/src/result.ts b/src/result.ts index 9d06624..338f7d7 100644 --- a/src/result.ts +++ b/src/result.ts @@ -14,8 +14,8 @@ export type ResultMatchAsync = { }; export class ResultImpl { - private readonly _ok: boolean; - private readonly _value: T | E; + protected readonly _ok: boolean; + protected readonly _value: T | E; public constructor(ok: boolean, value: T | E) { this._ok = ok; @@ -660,7 +660,7 @@ export class ResultImpl { * } * ``` */ - public unwrap(): T | null { + public unwrapUnchecked(): T | null { if (this._ok) { return this._value as T; } @@ -706,20 +706,20 @@ export class ResultImpl { * @example * ```typescript * const x: Result = Ok(2); - * assertEquals(x.unwrapErr(), null); + * assertEquals(x.unwrapErrUnchecked(), null); * * const y: Result = Err("emergency failure"); - * assertEquals(y.unwrapErr(), "emergency failure"); + * assertEquals(y.unwrapErrUnchecked(), "emergency failure"); * * const z = Result.fromThrowable(...); // Result * if (z.isErr()) { - * const e = z.unwrapErr() // `e` has type `E` + * const e = z.unwrapErrUnchecked() // `e` has type `E` * } else { * const u = z.unwrapErr() // `u` has type `null` * } * ``` */ - public unwrapErr(): E | null { + public unwrapErrUnchecked(): E | null { if (!this._ok) { return this._value as E; } @@ -1062,7 +1062,7 @@ export interface Ok extends ResultImpl { [symbols.tag]: "Ok"; unwrap(): T; - unwrapErr(): null; + unwrapUnchecked(): T; expect(message: string): T; expectErr(message: string): never; @@ -1078,18 +1078,30 @@ export interface Ok extends ResultImpl { error(): undefined; } +class OkImpl extends ResultImpl { + public unwrap(): T { + return this._value as T; + } +} + /** * Contains the success value. */ export function Ok(value: T): Ok { - return new ResultImpl(true, value) as Ok; + return new OkImpl(true, value) as Ok; +} + +class ErrImpl extends ResultImpl { + public unwrapErr(): E { + return this._value as E; + } } export interface Err extends ResultImpl { [symbols.tag]: "Err"; - unwrap(): null; - unwrapErr(): E; + unwrapUnchecked(): null; + unwrapErrUnchecked(): E; expect(message: string): never; expectErr(message: string): E; @@ -1113,7 +1125,9 @@ export interface Err extends ResultImpl { * Contains the error value. */ export function Err(error: E): Err { - return new ResultImpl(false, error) as Err; + const err = new ResultImpl(false, error) as Err; + err.unwrapErrUnchecked = () => error; + return err; } /** @@ -1242,3 +1256,6 @@ export namespace Result { */ export const from = fromThrowable; } + +// How to have Ok() and new Ok() +// https://www.typescriptlang.org/play/?#code/MYGwhgzhAECyCeA5ArgWwEYFMBO0DeAUNNAG5gjKYBc0AdmltgNwFHTAD2tEALtssB4dsACjIVqdBjgCU+NsR4ALAJYQAdOMrQAvKXKUWxAL6tiYACYWR9VDVuMZ96bkLFi2TD2TZa0ZWqaBpjQANRSqEbQpqYEAPRx0AAimABmKrQhYH4ZPDipYMAhymA87BwYGZgwyiGgkDCc3HwCQrjZFuzk4OggIanItIIqXAS5+YUhCCgYOADCXLz8gsLyxJkA7tBiwc6z2E5wSC5ROxJ7jjTTJ2wADtgcQjzwt5LX+yyxCdBznqXFSjq3TAvX6g2GoyavCOM0YADFwTwRn49AMhkiuNstJIHLIrsd9mtoJ5vL46Jgtu9GGdKDJPtBIDCXAtmss2ix4okAMpefyA6D3R6PF7FDjQTDcHwhDK8bJFGAQJRgTx8kIQMCoEKa5QcCwQAhUnAI9HI9SCp4i3RM-Zmh4W14c74AVXVAHNMAQoWUZTw5ZgAIxWzbW+GI5Eif0ABjp0GdEAyrugAHJNknPYsOH11CAOK6RD6-f71JZrABWGQx74AeWQPFutYgNH9pdYXugBaGmAATFbDdhjRDaCIu9GmLHEgB1FQ62vJ1Pp7iZzDZ3P55p+rvFqwR6OVxI1usNmgAZkjBCAA diff --git a/tests/async_result.test.ts b/tests/async_result.test.ts index af4ea45..cdfb7f2 100644 --- a/tests/async_result.test.ts +++ b/tests/async_result.test.ts @@ -122,14 +122,14 @@ describe("flatten", () => { const inner = TestOk(42); const flattened = TestOkPromise, boolean>(inner).flatten(); expectTypeOf(flattened).toEqualTypeOf>(); - await expect(flattened.unwrap()).resolves.toEqual(inner.unwrap()); + await expect(flattened.unwrap()).resolves.toEqual(inner.unwrapUnchecked()); }); it("works with an Ok result", async () => { const inner = TestErr("error"); const flattened = TestOkPromise, boolean>(inner).flatten(); expectTypeOf(flattened).toEqualTypeOf>(); - await expect(flattened.unwrapErr()).resolves.toEqual(inner.unwrapErr()); + await expect(flattened.unwrapErr()).resolves.toEqual(inner.unwrapErrUnchecked()); }); it("works with an Err result", async () => { @@ -231,7 +231,7 @@ describe("map", () => { const result = TestErrPromise(error); const result2 = result.map((value) => value * 2); const awaitedResult = await result; - await expect(result2.unwrapErr()).resolves.toEqual(awaitedResult.unwrapErr()); + await expect(result2.unwrapErr()).resolves.toEqual(awaitedResult.unwrapErrUnchecked()); }); }); @@ -246,7 +246,7 @@ describe("mapAsync", () => { const a = TestErrPromise("error"); const b = a.map((value) => value * 2); const awaitedResult = await a; - await expect(b.unwrapErr()).resolves.toEqual(awaitedResult.unwrapErr()); + await expect(b.unwrapErr()).resolves.toEqual(awaitedResult.unwrapErrUnchecked()); }); }); diff --git a/tests/fn.test.ts b/tests/fn.test.ts index 1f0fb5c..388856c 100644 --- a/tests/fn.test.ts +++ b/tests/fn.test.ts @@ -15,13 +15,13 @@ describe("fn", () => { it("returns Ok result when provided function does not throw", () => { const wrappedFn = fn(() => Ok(42)); const result = wrappedFn(); - expect(result.unwrap()).toEqual(42); + expect(result.unwrapUnchecked()).toEqual(42); }); it("returns Err result when provided function returns Err", () => { const wrappedFn = fn(() => Err("rekt")); const result = wrappedFn(); - expect(result.unwrapErr()).toEqual("rekt"); + expect(result.unwrapErrUnchecked()).toEqual("rekt"); }); describe("types", () => { @@ -143,13 +143,13 @@ describe("asyncFn", () => { it("returns Ok result when provided async function does not throw", async () => { const wrappedFn = asyncFn(async () => Promise.resolve(Ok(42))); const result = await wrappedFn(); - expect(result.unwrap()).toEqual(42); + expect(result.unwrapUnchecked()).toEqual(42); }); it("returns Err result when provided async function returns Err", async () => { const wrappedFn = asyncFn(async () => Promise.resolve(Err("rekt"))); const result = await wrappedFn(); - expect(result.unwrapErr()).toEqual("rekt"); + expect(result.unwrapErrUnchecked()).toEqual("rekt"); }); describe("types", () => { diff --git a/tests/option.test.ts b/tests/option.test.ts index 8e7902a..de99493 100644 --- a/tests/option.test.ts +++ b/tests/option.test.ts @@ -61,24 +61,24 @@ describe("core", () => { describe("okOr", () => { it("returns the value when called on a Some option", () => { const option = TestSome(42); - expect(option.okOr("error").unwrap()).toEqual(42); + expect(option.okOr("error").unwrapUnchecked()).toEqual(42); }); it("returns the error value when called on a None option", () => { const option = TestNone(); - expect(option.okOr("error").unwrapErr()).toEqual("error"); + expect(option.okOr("error").unwrapErrUnchecked()).toEqual("error"); }); }); describe("okOrElse", () => { it("returns the value when called on a Some option", () => { const option = TestSome(42); - expect(option.okOrElse(() => "error").unwrap()).toEqual(42); + expect(option.okOrElse(() => "error").unwrapUnchecked()).toEqual(42); }); it("returns the error value when called on a None option", () => { const option = TestNone(); - expect(option.okOrElse(() => "error").unwrapErr()).toEqual("error"); + expect(option.okOrElse(() => "error").unwrapErrUnchecked()).toEqual("error"); }); }); diff --git a/tests/result.test.ts b/tests/result.test.ts index 1a0a7d7..12fc407 100644 --- a/tests/result.test.ts +++ b/tests/result.test.ts @@ -27,7 +27,8 @@ describe("core", () => { expectTypeOf(r.error).toEqualTypeOf<() => undefined>(); expectTypeOf(r.unwrap).toEqualTypeOf<() => number>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => number>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => null>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => number>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => never>(); @@ -44,7 +45,8 @@ describe("core", () => { expectTypeOf(r.error).toEqualTypeOf<() => string>(); expectTypeOf(r.unwrap).toEqualTypeOf<() => null>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => string>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => string>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => never>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => string>(); @@ -52,14 +54,18 @@ describe("core", () => { it("works as discriminated union", () => { const r = TestOk(42); + + expectTypeOf(r.unwrap()).toEqualTypeOf<"rekt">(); expectTypeOf(r.value()).toEqualTypeOf(); expectTypeOf(r.error()).toEqualTypeOf(); + if (r.isOk()) { expectTypeOf(r.value).toEqualTypeOf<() => number>(); expectTypeOf(r.error).toEqualTypeOf<() => undefined>(); expectTypeOf(r.unwrap).toEqualTypeOf<() => number>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => number>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => null>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => number>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => never>(); @@ -68,7 +74,8 @@ describe("core", () => { expectTypeOf(r.error).toEqualTypeOf<() => string>(); expectTypeOf(r.unwrap).toEqualTypeOf<() => null>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => string>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => string>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => never>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => string>(); @@ -78,8 +85,8 @@ describe("core", () => { expectTypeOf(r.value).toEqualTypeOf<() => undefined>(); expectTypeOf(r.error).toEqualTypeOf<() => string>(); - expectTypeOf(r.unwrap).toEqualTypeOf<() => null>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => string>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => string>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => never>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => string>(); @@ -87,8 +94,8 @@ describe("core", () => { expectTypeOf(r.value).toEqualTypeOf<() => number>(); expectTypeOf(r.error).toEqualTypeOf<() => undefined>(); - expectTypeOf(r.unwrap).toEqualTypeOf<() => number>(); - expectTypeOf(r.unwrapErr).toEqualTypeOf<() => null>(); + expectTypeOf(r.unwrapUnchecked).toEqualTypeOf<() => number>(); + expectTypeOf(r.unwrapErrUnchecked).toEqualTypeOf<() => null>(); expectTypeOf(r.expect).toEqualTypeOf<(msg: string) => number>(); expectTypeOf(r.expectErr).toEqualTypeOf<(msg: string) => never>(); @@ -124,37 +131,39 @@ describe("and", () => { it("returns the error when Ok and Err", () => { const a = TestOk("a"); const b = TestErr("b"); - expect(a.and(b).unwrapErr()).toEqual("b"); + expect(a.and(b).unwrapErrUnchecked()).toEqual("b"); }); it("returns the late value when Ok and Ok", () => { const a = TestOk("a"); const b = TestOk("b"); - expect(a.and(b).unwrap()).toEqual("b"); + expect(a.and(b).unwrapUnchecked()).toEqual("b"); }); it("returns the error when Err and Ok", () => { const a = TestErr("a"); const b = TestOk("b"); - expect(a.and(b).unwrapErr()).toEqual("a"); + expect(a.and(b).unwrapErrUnchecked()).toEqual("a"); }); it("returns the early error when Err and Err", () => { const a = TestErr("a"); const b = TestErr("b"); - expect(a.and(b).unwrapErr()).toEqual("a"); + expect(a.and(b).unwrapErrUnchecked()).toEqual("a"); }); }); describe("andThen", () => { it("returns the mapped value for an Ok result", () => { const a = TestOk(0); - expect(a.andThen((value) => Ok(value + 1)).unwrap()).toEqual(1); + expect(a.andThen((value) => Ok(value + 1)).unwrapUnchecked()).toEqual(1); }); it("returns the result for an Err result", () => { const a = TestErr("early error"); - expect(a.andThen((value) => Ok(value + 1)).unwrapErr()).toEqual(a.unwrapErr()); + expect(a.andThen((value) => Ok(value + 1)).unwrapErrUnchecked()).toEqual( + a.unwrapErrUnchecked(), + ); }); }); @@ -167,7 +176,7 @@ describe("andThenAsync", () => { it("returns the result for an Err result", async () => { const a = TestErr("early error"); await expect(a.andThenAsync(async (value) => Ok(value + 1)).unwrapErr()).resolves.toEqual( - a.unwrapErr(), + a.unwrapErrUnchecked(), ); }); }); @@ -206,20 +215,20 @@ describe("flatten", () => { const inner = TestOk(42); const flattened = TestOk, boolean>(inner).flatten(); expectTypeOf(flattened).toEqualTypeOf>(); - expect(flattened.unwrap()).toEqual(inner.unwrap()); + expect(flattened.unwrapUnchecked()).toEqual(inner.unwrapUnchecked()); }); it("works with an Ok result", () => { const inner = TestErr("error"); const flattened = TestOk, boolean>(inner).flatten(); expectTypeOf(flattened).toEqualTypeOf>(); - expect(flattened.unwrapErr()).toEqual(inner.unwrapErr()); + expect(flattened.unwrapErrUnchecked()).toEqual(inner.unwrapErrUnchecked()); }); it("works with an Err result", () => { const flattened = TestErr, boolean>(true).flatten(); expectTypeOf(flattened).toEqualTypeOf>(); - expect(flattened.unwrapErr()).toEqual(true); + expect(flattened.unwrapErrUnchecked()).toEqual(true); }); it("works with non-primitive value or error", () => { @@ -308,13 +317,13 @@ describe("map", () => { it("returns the mapped value for an Ok result", () => { const result = TestOk(42); const result2 = result.map((value) => value * 2); - expect(result2.unwrap()).toEqual(84); + expect(result2.unwrapUnchecked()).toEqual(84); }); it("returns the original Err for an Err result", () => { const result = TestErr("error"); const result2 = result.map((value) => value * 2); - expect(result2.unwrapErr()).toEqual(result.unwrapErr()); + expect(result2.unwrapErrUnchecked()).toEqual(result.unwrapErrUnchecked()); }); }); @@ -328,7 +337,7 @@ describe("mapAsync", () => { it("returns the original Err for an Err result", async () => { const a = TestErr("error"); const b = a.mapAsync(async (value) => value * 2); - await expect(b.unwrapErr()).resolves.toEqual(a.unwrapErr()); + await expect(b.unwrapErr()).resolves.toEqual(a.unwrapErrUnchecked()); }); }); @@ -336,13 +345,13 @@ describe("mapErr", () => { it("returns the mapped error for an Err result", () => { const a = TestErr("error"); const b = a.mapErr(() => "new error"); - expect(b.unwrapErr()).toEqual("new error"); + expect(b.unwrapErrUnchecked()).toEqual("new error"); }); it("returns the original Ok for an Err result", () => { const a = TestOk(0); const b = a.mapErr(() => "new error"); - expect(b.unwrap()).toEqual(0); + expect(b.unwrapUnchecked()).toEqual(0); }); }); @@ -356,7 +365,7 @@ describe("mapErrAsync", () => { it("returns the original Ok for an Err result", async () => { const a = TestOk(0); const b = a.mapErrAsync(async () => "new error"); - await expect(b.unwrap()).resolves.toEqual(a.unwrap()); + await expect(b.unwrap()).resolves.toEqual(a.unwrapUnchecked()); }); }); @@ -398,38 +407,38 @@ describe("or", () => { it("returns the value when Ok or Err", () => { const a = TestOk("a"); const b = TestErr("b"); - expect(a.or(b).unwrap()).toEqual("a"); + expect(a.or(b).unwrapUnchecked()).toEqual("a"); }); it("returns the early value when Ok or Ok", () => { const a = TestOk("a"); const b = TestOk("b"); - expect(a.or(b).unwrap()).toEqual("a"); + expect(a.or(b).unwrapUnchecked()).toEqual("a"); }); it("returns the late value when Err or Ok", () => { const a = TestErr("a"); const b = TestOk("b"); - expect(a.or(b).unwrap()).toEqual("b"); + expect(a.or(b).unwrapUnchecked()).toEqual("b"); }); it("returns the late error when Err and Err", () => { const a = TestErr("a"); const b = TestErr("b"); - expect(a.or(b).unwrapErr()).toEqual("b"); + expect(a.or(b).unwrapErrUnchecked()).toEqual("b"); }); }); describe("orElse", () => { it("returns the result for an Ok result", () => { const a = TestOk(0); - expect(a.orElse(() => Ok(1)).unwrap()).toEqual(a.unwrap()); + expect(a.orElse(() => Ok(1)).unwrapUnchecked()).toEqual(a.unwrapUnchecked()); }); it("returns the mapped value for an Err result", () => { const a = TestErr("early error"); - expect(a.orElse(() => Ok(1)).unwrap()).toEqual(1); - expect(a.orElse(() => Err(1)).unwrapErr()).toEqual(1); + expect(a.orElse(() => Ok(1)).unwrapUnchecked()).toEqual(1); + expect(a.orElse(() => Err(1)).unwrapErrUnchecked()).toEqual(1); }); }); @@ -449,24 +458,24 @@ describe("orElseAsync", () => { describe("unwrap", () => { it("returns the value for an Ok result", () => { const result = TestOk(42); - expect(result.unwrap()).toEqual(42); + expect(result.unwrapUnchecked()).toEqual(42); }); it("returns null for an Err result", () => { const result = TestErr("error"); - expect(result.unwrap()).toEqual(null); + expect(result.unwrapUnchecked()).toEqual(null); }); }); describe("unwrapErr", () => { it("returns the error for an Err result", () => { const result = TestErr("error"); - expect(result.unwrapErr()).toEqual("error"); + expect(result.unwrapErrUnchecked()).toEqual("error"); }); it("returns null for an Ok result", () => { const result = TestOk(42); - expect(result.unwrapErr()).toEqual(null); + expect(result.unwrapErrUnchecked()).toEqual(null); }); }); @@ -524,8 +533,8 @@ describe("iterator", () => { } const result = gen(); - expect(result.next().value.unwrap()).toEqual(1); - expect(result.next().value.unwrapErr()).toEqual("error"); - expect(result.next().value.unwrap()).toEqual(2); + expect(result.next().value.unwrapUnchecked()).toEqual(1); + expect(result.next().value.unwrapErrUnchecked()).toEqual("error"); + expect(result.next().value.unwrapUnchecked()).toEqual(2); }); }); diff --git a/tests/try.test.ts b/tests/try.test.ts index a58df7c..1da1a27 100644 --- a/tests/try.test.ts +++ b/tests/try.test.ts @@ -23,7 +23,7 @@ describe("deprecated try()", () => { return Ok(x + y); }); expectTypeOf(block).toEqualTypeOf>(); - expect(block.unwrap()).toEqual(2); + expect(block.unwrapUnchecked()).toEqual(2); const block4 = tryBlock(function* () { yield* Err("error").try(); @@ -42,7 +42,7 @@ describe("deprecated try()", () => { return Ok(x + y); }); expectTypeOf(block2).toEqualTypeOf>(); - expect(block2.unwrapErr()).toEqual("error"); + expect(block2.unwrapErrUnchecked()).toEqual("error"); const block3 = tryBlock(function* () { const x = yield* Ok(1).try(); @@ -155,7 +155,7 @@ test("tryBlock", () => { return Ok(x + y); }); expectTypeOf(block).toEqualTypeOf>(); - expect(block.unwrap()).toEqual(2); + expect(block.unwrapUnchecked()).toEqual(2); const block4 = tryBlock(function* () { yield* Err("error"); @@ -174,7 +174,7 @@ test("tryBlock", () => { return Ok(x + y); }); expectTypeOf(block2).toEqualTypeOf>(); - expect(block2.unwrapErr()).toEqual("error"); + expect(block2.unwrapErrUnchecked()).toEqual("error"); const block3 = tryBlock(function* () { const x = yield* Ok(1); diff --git a/tests/unwind.test.ts b/tests/unwind.test.ts index b1e2362..3651470 100644 --- a/tests/unwind.test.ts +++ b/tests/unwind.test.ts @@ -13,7 +13,7 @@ describe("catchUnwind", () => { it("returns Ok result when function succeeds", () => { const result = catchUnwind(() => 42); expectTypeOf(result).toEqualTypeOf>(); - expect(result.unwrap()).toEqual(42); + expect(result.unwrapUnchecked()).toEqual(42); }); it("catches regular Error and returns it as-is in Err result", () => { @@ -21,7 +21,7 @@ describe("catchUnwind", () => { const result = catchUnwind(() => { throw error; }); - expect(result.unwrapErr()).toBe(error); + expect(result.unwrapErrUnchecked()).toBe(error); }); it("converts Panic to Error while preserving message and cause", () => { @@ -29,7 +29,7 @@ describe("catchUnwind", () => { const result = catchUnwind(() => { throw panic; }); - const err = result.unwrapErr(); + const err = result.unwrapErrUnchecked(); expect(err).toBeInstanceOf(Error); expect(err?.message).toEqual("test panic"); expect((err as Error & { cause?: unknown }).cause).toBe(panic); @@ -39,7 +39,7 @@ describe("catchUnwind", () => { const result = catchUnwind(() => { throw "string error"; }); - const err = result.unwrapErr(); + const err = result.unwrapErrUnchecked(); expect(err).toBeInstanceOf(Error); expect(err?.message).toContain(UNEXPECTED_ERROR_MESSAGE); expect((err as Error & { cause?: unknown }).cause).toEqual("string error"); @@ -67,7 +67,7 @@ describe("catchUnwindAsync", () => { const result = await catchUnwindAsync(async () => { throw panic; }); - const err = result.unwrapErr(); + const err = result.unwrapErrUnchecked(); expect(err).toBeInstanceOf(Error); expect(err?.message).toEqual("test panic"); expect((err as Error & { cause?: unknown }).cause).toBe(panic);