From 2842549deb626942d9f0b4750ba90e1958531817 Mon Sep 17 00:00:00 2001 From: Connor Sinnott Date: Sat, 7 Sep 2024 11:15:29 -0700 Subject: [PATCH] Correct partition signature --- src/result.ts | 2 +- test/result.test.ts | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/result.ts b/src/result.ts index f1fba31..ed3994f 100644 --- a/src/result.ts +++ b/src/result.ts @@ -545,7 +545,7 @@ export namespace Result { /** * Partitions a set of results, separating the `Ok` and `Err` values. */ - export function partition[]>(results: T): [ResultOkTypes, ResultErrTypes] { + export function partition[]>(results: T): [ResultOkTypes, ResultErrTypes] { return results.reduce( ([oks, errors], v) => v.isOk() diff --git a/test/result.test.ts b/test/result.test.ts index 7332940..619cf65 100644 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -48,7 +48,10 @@ test('andThen', () => { const result = new Ok('Ok') as Result; const then = result.andThen(() => new Err('broke') as Result); expect(then).toMatchResult(new Err('broke')); - function takesResult(result: Result): void {} + + function takesResult(result: Result): void { + } + takesResult(then); }); @@ -200,6 +203,7 @@ test('Result.wrap', () => { class CustomError { readonly message = 'hi'; } + const err = new CustomError(); const b = Result.wrap(() => { @@ -217,6 +221,7 @@ test('Result.wrapAsync', async () => { class CustomError { readonly message = 'hi'; } + const err = new CustomError(); const b = await Result.wrapAsync(async () => { @@ -239,29 +244,35 @@ test('Result.partition', async () => { const ok1 = new Ok(true); const err0 = Err(Symbol()); const err1 = new Err(Error()); + const result0 = Ok(3) as unknown as Result; + const result1 = new Ok(true) as unknown as Result; const all0 = Result.partition([]); expect(all0).toEqual([[], []]); - eq(true); + eq(true); const all1 = Result.partition([ok0, ok1, err0, err1]); expect(all1).toEqual([ [ok0.value, ok1.value], [err0.error, err1.error], ]); - eq(true); + eq(true); const all2 = Result.partition([ok0, ok1]); expect(all2).toEqual([[ok0.value, ok1.value], []]); - eq(true); + eq(true); const all3 = Result.partition([err0, err1]); expect(all3).toEqual([[], [err0.error, err1.error]]); - eq(true); + eq(true); const all4 = Result.partition([1, 2, 3, 4].map((num) => Ok(num) as Result)); expect(all4).toEqual([[1, 2, 3, 4], []]); eq(true); + + const all5 = Result.partition([result0, result1]); + expect(all5).toEqual([[(result0 as Ok).value, (result1 as Ok).value], []]); + eq(true); }); test('safeUnwrap', () => {