diff --git a/.changeset/thirty-grapes-drum.md b/.changeset/thirty-grapes-drum.md new file mode 100644 index 00000000..10867d68 --- /dev/null +++ b/.changeset/thirty-grapes-drum.md @@ -0,0 +1,5 @@ +--- +"neverthrow": patch +--- + +Fix `combineWithAllErrors` types diff --git a/src/result-async.ts b/src/result-async.ts index 13f9ce7d..91e93835 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -278,22 +278,13 @@ type TraverseAsync = IsLiteralArray extends 1 : never // This type is similar to the `TraverseAsync` while the errors are also -// collected in order. For the checks/conditions made here, see that type +// collected in a list. For the checks/conditions made here, see that type // for the documentation. -type TraverseWithAllErrorsAsync = IsLiteralArray extends 1 - ? Combine extends [infer Oks, infer Errs] - ? ResultAsync, EmptyArrayToNever> - : never - : Writable extends Array - ? Combine, Depth> extends [infer Oks, infer Errs] - ? Oks extends unknown[] - ? Errs extends unknown[] - ? ResultAsync, EmptyArrayToNever> - : ResultAsync, Errs> - : Errs extends unknown[] - ? ResultAsync> - : ResultAsync - : never +type TraverseWithAllErrorsAsync = TraverseAsync< + T, + Depth +> extends ResultAsync + ? ResultAsync : never // Converts a reaodnly array into a writable array diff --git a/src/result.ts b/src/result.ts index 3a00d901..70b1fd57 100644 --- a/src/result.ts +++ b/src/result.ts @@ -649,11 +649,11 @@ type Traverse = Combine extends [infer Ok // Traverses an array of results and returns a single result containing // the oks combined and the array of errors combined. -type TraverseWithAllErrors = Combine extends [ +type TraverseWithAllErrors = Traverse extends Result< infer Oks, - infer Errs, -] - ? Result, EmptyArrayToNever> + infer Errs +> + ? Result : never // Combines the array of results into one result. diff --git a/tests/index.test.ts b/tests/index.test.ts index 81f17157..41f1b75d 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -711,7 +711,7 @@ describe('Utils', () => { okAsync(true), ] - type ExpecteResult = Result<[ string, number, boolean ], [string, number, boolean]> + type ExpecteResult = Result<[ string, number, boolean ], (string | number | boolean)[]> const result: ExpecteResult = await ResultAsync.combineWithAllErrors(heterogenousList) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index 28b9c1b1..3ca0614c 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -1177,7 +1177,7 @@ type CreateTuple = (function describe(_ = 'combineWithAllErrors') { (function it(_ = 'combines different results into one') { - type Expectation = Result<[ number, string, never, never ], [never, never, string[], Error]>; + type Expectation = Result<[ number, string, never, never ], (string[] | Error)[]>; const result = Result.combineWithAllErrors([ ok(1), @@ -1191,7 +1191,7 @@ type CreateTuple = }); (function it(_ = 'combines only ok results into one') { - type Expectation = Result<[ number, string ], [never, never]>; + type Expectation = Result<[ number, string ], never[]>; const result = Result.combineWithAllErrors([ ok(1), @@ -1203,7 +1203,7 @@ type CreateTuple = }); (function it(_ = 'combines only err results into one') { - type Expectation = Result<[ never, never ], [number, 'string']>; + type Expectation = Result<[ never, never ], (number | 'string')[]>; const result = Result.combineWithAllErrors([ err(1), @@ -1224,10 +1224,20 @@ type CreateTuple = const assignablefromCheck: typeof result = assignableToCheck; }); + (function it(_ = 'combines arrays of different results to a result of an array') { + type Expectation = Result<(string | boolean)[], (number | string)[]>; + const results: (Result | Result)[] = []; + + const result = Result.combineWithAllErrors(results); + + const assignableToCheck: Expectation = result; + const assignablefromCheck: typeof result = assignableToCheck; + }); + (function describe(_ = 'inference on large tuples') { (function it(_ = 'Should correctly infer the type on tuples with 6 elements') { type Input = CreateTuple<6, Result> - type Expectation = Result, CreateTuple<6, number>> + type Expectation = Result, number[]> const inputValues = input() const result = Result.combineWithAllErrors(inputValues) @@ -1239,7 +1249,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 15 elements') { type Input = CreateTuple<15, Result> - type Expectation = Result, CreateTuple<15, number>> + type Expectation = Result, number[]> const inputValues = input() const result = Result.combineWithAllErrors(inputValues) @@ -1251,7 +1261,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 30 elements') { type Input = CreateTuple<30, Result> - type Expectation = Result, CreateTuple<30, number>> + type Expectation = Result, number[]> const inputValues = input() const result = Result.combineWithAllErrors(inputValues) @@ -1263,7 +1273,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 49 elements') { type Input = CreateTuple<49 , Result> - type Expectation = Result, CreateTuple<49, number>> + type Expectation = Result, number[]> const inputValues = input() const result = Result.combineWithAllErrors(inputValues) @@ -2199,7 +2209,7 @@ type CreateTuple = (function describe(_ = 'combineWithAllErrors') { (function it(_ = 'combines different result asyncs into one') { - type Expectation = ResultAsync<[ number, string, never, never ], [never, never, string[], Error]>; + type Expectation = ResultAsync<[ number, string, never, never ], (string[] | Error)[]>; const result = ResultAsync.combineWithAllErrors([ okAsync(1), @@ -2213,7 +2223,7 @@ type CreateTuple = }); (function it(_ = 'combines only ok result asyncs into one') { - type Expectation = ResultAsync<[ number, string ], [never, never]>; + type Expectation = ResultAsync<[ number, string ], never[]>; const result = ResultAsync.combineWithAllErrors([ okAsync(1), @@ -2225,7 +2235,7 @@ type CreateTuple = }); (function it(_ = 'combines only err result asyncs into one') { - type Expectation = ResultAsync<[ never, never ], [number, string]>; + type Expectation = ResultAsync<[ never, never ], (number | string)[]>; const result = ResultAsync.combineWithAllErrors([ errAsync(1), @@ -2246,10 +2256,20 @@ type CreateTuple = const assignablefromCheck: typeof result = assignableToCheck; }); + (function it(_ = 'combines arrays of different result asyncs to a result of an array') { + type Expectation = ResultAsync<(string | boolean)[], (number | string)[]>; + const results: (ResultAsync | ResultAsync)[] = []; + + const result = ResultAsync.combineWithAllErrors(results); + + const assignableToCheck: Expectation = result; + const assignablefromCheck: typeof result = assignableToCheck; + }); + (function describe(_ = 'inference on large tuples') { (function it(_ = 'Should correctly infer the type on tuples with 6 elements') { type Input = CreateTuple<6, ResultAsync> - type Expectation = ResultAsync, CreateTuple<6, number>> + type Expectation = ResultAsync, number[]> const inputValues = input() const result = ResultAsync.combineWithAllErrors(inputValues) @@ -2261,7 +2281,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 15 elements') { type Input = CreateTuple<15, ResultAsync> - type Expectation = ResultAsync, CreateTuple<15, number>> + type Expectation = ResultAsync, number[]> const inputValues = input() const result = ResultAsync.combineWithAllErrors(inputValues) @@ -2273,7 +2293,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 30 elements') { type Input = CreateTuple<30, ResultAsync> - type Expectation = ResultAsync, CreateTuple<30, number>> + type Expectation = ResultAsync, number[]> const inputValues = input() const result = ResultAsync.combineWithAllErrors(inputValues) @@ -2285,7 +2305,7 @@ type CreateTuple = (function it(_ = 'Should correctly infer the type on tuples with 49 elements') { type Input = CreateTuple<49 , ResultAsync> - type Expectation = ResultAsync, CreateTuple<49, number>> + type Expectation = ResultAsync, number[]> const inputValues = input() const result = ResultAsync.combineWithAllErrors(inputValues)