Skip to content

Commit 036a6b2

Browse files
committed
Refining types & tests around unwrap, unwrapErr, and expect
1 parent 370c00f commit 036a6b2

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/index.test.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ describe("ok input", () => {
154154

155155
test("unwrapping error throws", () => {
156156
try {
157-
// @ts-expect-error Test
158-
RsResult.unwrapErr(inputResult);
157+
const result = RsResult.unwrapErr(inputResult);
158+
159+
expectTypeOf(result).toMatchTypeOf<never>();
160+
161+
// Ensure that unwrapErr throws
159162
expect.fail("Should have thrown");
160163
} catch (e) {
161164
expect(e).toStrictEqual(new Error(`Unwrapping an ok result: 123`));
@@ -249,7 +252,11 @@ describe("err input", () => {
249252

250253
test("unwrapping throws", () => {
251254
try {
252-
RsResult.unwrap(inputResult);
255+
const result = RsResult.unwrap(inputResult);
256+
257+
expectTypeOf(result).toMatchTypeOf<never>();
258+
259+
// Ensure that unwrap throws
253260
expect.fail("Should have thrown");
254261
} catch (e) {
255262
expect(e).toStrictEqual(
@@ -260,7 +267,11 @@ describe("err input", () => {
260267

261268
test("expecting throws", () => {
262269
try {
263-
RsResult.expect(inputResult, "Custom error message");
270+
const result = RsResult.expect(inputResult, "Custom error message");
271+
272+
expectTypeOf(result).toMatchTypeOf<never>();
273+
274+
// Ensure that expect throws
264275
expect.fail("Should have thrown");
265276
} catch (e) {
266277
expect(e).toStrictEqual(
@@ -303,10 +314,14 @@ describe("non result input", () => {
303314

304315
test("unwrapping throws", () => {
305316
try {
306-
RsResult.unwrap(
317+
const result = RsResult.unwrap(
307318
// @ts-expect-error Invalid typed value for the test
308319
123
309320
);
321+
322+
expectTypeOf(result).toMatchTypeOf<never>();
323+
324+
// Ensure that unwrap throws
310325
expect.fail("Should have thrown");
311326
} catch (e) {
312327
expect(e).toStrictEqual(new Error(`Unwrapping a non-result value: 123`));
@@ -315,11 +330,15 @@ describe("non result input", () => {
315330

316331
test("expecting throws", () => {
317332
try {
318-
RsResult.expect(
333+
const result = RsResult.expect(
319334
// @ts-expect-error Invalid typed value for the test
320335
123,
321336
"Custom error message"
322337
);
338+
339+
expectTypeOf(result).toMatchTypeOf<never>();
340+
341+
// Ensure that unwrap throws
323342
expect.fail("Should have thrown");
324343
} catch (e) {
325344
expect(e).toStrictEqual(new Error(`Unwrapping a non-result value: 123`));
@@ -328,8 +347,14 @@ describe("non result input", () => {
328347

329348
test("unwrapping error throws", () => {
330349
try {
331-
// @ts-expect-error Test
332-
RsResult.unwrapErr(123);
350+
const result = RsResult.unwrapErr(
351+
// @ts-expect-error Test
352+
123
353+
);
354+
355+
expectTypeOf(result).toMatchTypeOf<never>();
356+
357+
// Ensure that unwrap throws
333358
expect.fail("Should have thrown");
334359
} catch (e) {
335360
expect(e).toStrictEqual(new Error(`Unwrapping a non-result value: 123`));

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,35 @@ export function expect<T>(result: Result<T, unknown>, message: string): T {
225225
return result.Ok;
226226
}
227227

228+
/**
229+
* Retrieve the value from an `Err` result.
230+
*
231+
* This override documents that an error is thrown when unwrapping an `Err` result.
232+
*
233+
* @param result An `Err` result value
234+
* @returns Never
235+
*/
236+
export function unwrapErr(result: Result<unknown, never>): never;
237+
238+
/**
239+
* Retrieve the error from an `Ok` result.
240+
*
241+
* This override is required for type safety for `Ok` results.
242+
*
243+
* @template E - The error type of the `Err` result.
244+
* @param result Result to unwrap the value from
245+
* @returns The error value of `E` result
246+
*/
247+
export function unwrapErr<E = unknown>(result: Result<unknown, E>): E;
248+
228249
/**
229250
* Retrieve the underlying error from an `Err` result.
230251
*
231252
* @template E - The error type of the `Err` result.
232253
* @param result Result to unwrap the error from
233254
* @returns The underlying error of the `Err` result
234255
*/
235-
export function unwrapErr<E = unknown>(result: Result<never, E>): E {
256+
export function unwrapErr<E = unknown>(result: Result<unknown, E>): E {
236257
if (!isResult(result)) {
237258
throw new Error(`Unwrapping a non-result value: ${JSON.stringify(result)}`);
238259
}

0 commit comments

Comments
 (0)