Skip to content

Commit

Permalink
Correct partition signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorSinnott committed Sep 7, 2024
1 parent 8019469 commit 2842549
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export namespace Result {
/**
* Partitions a set of results, separating the `Ok` and `Err` values.
*/
export function partition<const T extends Result<any, any>[]>(results: T): [ResultOkTypes<T>, ResultErrTypes<T>] {
export function partition<T extends Result<any, any>[]>(results: T): [ResultOkTypes<T>, ResultErrTypes<T>] {
return results.reduce(
([oks, errors], v) =>
v.isOk()
Expand Down
21 changes: 16 additions & 5 deletions test/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ test('andThen', () => {
const result = new Ok('Ok') as Result<string, boolean>;
const then = result.andThen(() => new Err('broke') as Result<boolean, string>);
expect(then).toMatchResult(new Err('broke'));
function takesResult(result: Result<boolean, string | boolean>): void {}

function takesResult(result: Result<boolean, string | boolean>): void {
}

takesResult(then);
});

Expand Down Expand Up @@ -200,6 +203,7 @@ test('Result.wrap', () => {
class CustomError {
readonly message = 'hi';
}

const err = new CustomError();

const b = Result.wrap<number, CustomError>(() => {
Expand All @@ -217,6 +221,7 @@ test('Result.wrapAsync', async () => {
class CustomError {
readonly message = 'hi';
}

const err = new CustomError();

const b = await Result.wrapAsync<number, CustomError>(async () => {
Expand All @@ -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<number, symbol>;
const result1 = new Ok(true) as unknown as Result<boolean, Error>;

const all0 = Result.partition([]);
expect(all0).toEqual([[], []]);
eq<typeof all0, [[], []]>(true);
eq<typeof all0, [never[], never[]]>(true);

const all1 = Result.partition([ok0, ok1, err0, err1]);
expect(all1).toEqual([
[ok0.value, ok1.value],
[err0.error, err1.error],
]);
eq<typeof all1, [[number, boolean, never, never], [never, never, symbol, Error]]>(true);
eq<typeof all1, [(number | boolean)[], (symbol | Error)[]]>(true);

const all2 = Result.partition([ok0, ok1]);
expect(all2).toEqual([[ok0.value, ok1.value], []]);
eq<typeof all2, [[number, boolean], [never, never]]>(true);
eq<typeof all2, [(number | boolean)[], never[]]>(true);

const all3 = Result.partition([err0, err1]);
expect(all3).toEqual([[], [err0.error, err1.error]]);
eq<typeof all3, [[never, never], [symbol, Error]]>(true);
eq<typeof all3, [never[], (symbol | Error)[]]>(true);

const all4 = Result.partition([1, 2, 3, 4].map((num) => Ok(num) as Result<number, Error>));
expect(all4).toEqual([[1, 2, 3, 4], []]);
eq<typeof all4, [number[], Error[]]>(true);

const all5 = Result.partition([result0, result1]);
expect(all5).toEqual([[(result0 as Ok<number>).value, (result1 as Ok<boolean>).value], []]);
eq<typeof all5, [(number | boolean)[], (symbol | Error)[]]>(true);
});

test('safeUnwrap', () => {
Expand Down

0 comments on commit 2842549

Please sign in to comment.