Skip to content

Commit 5121236

Browse files
authored
Merge pull request #483 from braxtonhall/fix-combine-with-all-errors
Fix combineWithAllErrors types
2 parents e72e9cd + ecc25b9 commit 5121236

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

.changeset/thirty-grapes-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"neverthrow": patch
3+
---
4+
5+
Fix `combineWithAllErrors` types

src/result-async.ts

+6-15
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,13 @@ type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1
278278
: never
279279

280280
// This type is similar to the `TraverseAsync` while the errors are also
281-
// collected in order. For the checks/conditions made here, see that type
281+
// collected in a list. For the checks/conditions made here, see that type
282282
// for the documentation.
283-
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1
284-
? Combine<T, Depth> extends [infer Oks, infer Errs]
285-
? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>>
286-
: never
287-
: Writable<T> extends Array<infer I>
288-
? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs]
289-
? Oks extends unknown[]
290-
? Errs extends unknown[]
291-
? ResultAsync<EmptyArrayToNever<Oks[number][]>, EmptyArrayToNever<Errs[number][]>>
292-
: ResultAsync<EmptyArrayToNever<Oks[number][]>, Errs>
293-
: Errs extends unknown[]
294-
? ResultAsync<Oks, EmptyArrayToNever<Errs[number][]>>
295-
: ResultAsync<Oks, Errs>
296-
: never
283+
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = TraverseAsync<
284+
T,
285+
Depth
286+
> extends ResultAsync<infer Oks, infer Errs>
287+
? ResultAsync<Oks, Errs[]>
297288
: never
298289

299290
// Converts a reaodnly array into a writable array

src/result.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,11 @@ type Traverse<T, Depth extends number = 5> = Combine<T, Depth> extends [infer Ok
649649

650650
// Traverses an array of results and returns a single result containing
651651
// the oks combined and the array of errors combined.
652-
type TraverseWithAllErrors<T, Depth extends number = 5> = Combine<T, Depth> extends [
652+
type TraverseWithAllErrors<T, Depth extends number = 5> = Traverse<T, Depth> extends Result<
653653
infer Oks,
654-
infer Errs,
655-
]
656-
? Result<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>>
654+
infer Errs
655+
>
656+
? Result<Oks, Errs[]>
657657
: never
658658

659659
// Combines the array of results into one result.

tests/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ describe('Utils', () => {
711711
okAsync(true),
712712
]
713713

714-
type ExpecteResult = Result<[ string, number, boolean ], [string, number, boolean]>
714+
type ExpecteResult = Result<[ string, number, boolean ], (string | number | boolean)[]>
715715

716716
const result: ExpecteResult = await ResultAsync.combineWithAllErrors(heterogenousList)
717717

tests/typecheck-tests.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ type CreateTuple<L, V = string> =
11771177

11781178
(function describe(_ = 'combineWithAllErrors') {
11791179
(function it(_ = 'combines different results into one') {
1180-
type Expectation = Result<[ number, string, never, never ], [never, never, string[], Error]>;
1180+
type Expectation = Result<[ number, string, never, never ], (string[] | Error)[]>;
11811181

11821182
const result = Result.combineWithAllErrors([
11831183
ok(1),
@@ -1191,7 +1191,7 @@ type CreateTuple<L, V = string> =
11911191
});
11921192

11931193
(function it(_ = 'combines only ok results into one') {
1194-
type Expectation = Result<[ number, string ], [never, never]>;
1194+
type Expectation = Result<[ number, string ], never[]>;
11951195

11961196
const result = Result.combineWithAllErrors([
11971197
ok(1),
@@ -1203,7 +1203,7 @@ type CreateTuple<L, V = string> =
12031203
});
12041204

12051205
(function it(_ = 'combines only err results into one') {
1206-
type Expectation = Result<[ never, never ], [number, 'string']>;
1206+
type Expectation = Result<[ never, never ], (number | 'string')[]>;
12071207

12081208
const result = Result.combineWithAllErrors([
12091209
err(1),
@@ -1224,10 +1224,20 @@ type CreateTuple<L, V = string> =
12241224
const assignablefromCheck: typeof result = assignableToCheck;
12251225
});
12261226

1227+
(function it(_ = 'combines arrays of different results to a result of an array') {
1228+
type Expectation = Result<(string | boolean)[], (number | string)[]>;
1229+
const results: (Result<string, number> | Result<boolean, string>)[] = [];
1230+
1231+
const result = Result.combineWithAllErrors(results);
1232+
1233+
const assignableToCheck: Expectation = result;
1234+
const assignablefromCheck: typeof result = assignableToCheck;
1235+
});
1236+
12271237
(function describe(_ = 'inference on large tuples') {
12281238
(function it(_ = 'Should correctly infer the type on tuples with 6 elements') {
12291239
type Input = CreateTuple<6, Result<string, number>>
1230-
type Expectation = Result<CreateTuple<6, string>, CreateTuple<6, number>>
1240+
type Expectation = Result<CreateTuple<6, string>, number[]>
12311241

12321242
const inputValues = input<Input>()
12331243
const result = Result.combineWithAllErrors(inputValues)
@@ -1239,7 +1249,7 @@ type CreateTuple<L, V = string> =
12391249

12401250
(function it(_ = 'Should correctly infer the type on tuples with 15 elements') {
12411251
type Input = CreateTuple<15, Result<string, number>>
1242-
type Expectation = Result<CreateTuple<15, string>, CreateTuple<15, number>>
1252+
type Expectation = Result<CreateTuple<15, string>, number[]>
12431253

12441254
const inputValues = input<Input>()
12451255
const result = Result.combineWithAllErrors(inputValues)
@@ -1251,7 +1261,7 @@ type CreateTuple<L, V = string> =
12511261

12521262
(function it(_ = 'Should correctly infer the type on tuples with 30 elements') {
12531263
type Input = CreateTuple<30, Result<string, number>>
1254-
type Expectation = Result<CreateTuple<30, string>, CreateTuple<30, number>>
1264+
type Expectation = Result<CreateTuple<30, string>, number[]>
12551265

12561266
const inputValues = input<Input>()
12571267
const result = Result.combineWithAllErrors(inputValues)
@@ -1263,7 +1273,7 @@ type CreateTuple<L, V = string> =
12631273

12641274
(function it(_ = 'Should correctly infer the type on tuples with 49 elements') {
12651275
type Input = CreateTuple<49 , Result<string, number>>
1266-
type Expectation = Result<CreateTuple<49, string>, CreateTuple<49, number>>
1276+
type Expectation = Result<CreateTuple<49, string>, number[]>
12671277

12681278
const inputValues = input<Input>()
12691279
const result = Result.combineWithAllErrors(inputValues)
@@ -2199,7 +2209,7 @@ type CreateTuple<L, V = string> =
21992209

22002210
(function describe(_ = 'combineWithAllErrors') {
22012211
(function it(_ = 'combines different result asyncs into one') {
2202-
type Expectation = ResultAsync<[ number, string, never, never ], [never, never, string[], Error]>;
2212+
type Expectation = ResultAsync<[ number, string, never, never ], (string[] | Error)[]>;
22032213

22042214
const result = ResultAsync.combineWithAllErrors([
22052215
okAsync(1),
@@ -2213,7 +2223,7 @@ type CreateTuple<L, V = string> =
22132223
});
22142224

22152225
(function it(_ = 'combines only ok result asyncs into one') {
2216-
type Expectation = ResultAsync<[ number, string ], [never, never]>;
2226+
type Expectation = ResultAsync<[ number, string ], never[]>;
22172227

22182228
const result = ResultAsync.combineWithAllErrors([
22192229
okAsync(1),
@@ -2225,7 +2235,7 @@ type CreateTuple<L, V = string> =
22252235
});
22262236

22272237
(function it(_ = 'combines only err result asyncs into one') {
2228-
type Expectation = ResultAsync<[ never, never ], [number, string]>;
2238+
type Expectation = ResultAsync<[ never, never ], (number | string)[]>;
22292239

22302240
const result = ResultAsync.combineWithAllErrors([
22312241
errAsync(1),
@@ -2246,10 +2256,20 @@ type CreateTuple<L, V = string> =
22462256
const assignablefromCheck: typeof result = assignableToCheck;
22472257
});
22482258

2259+
(function it(_ = 'combines arrays of different result asyncs to a result of an array') {
2260+
type Expectation = ResultAsync<(string | boolean)[], (number | string)[]>;
2261+
const results: (ResultAsync<string, number> | ResultAsync<boolean, string>)[] = [];
2262+
2263+
const result = ResultAsync.combineWithAllErrors(results);
2264+
2265+
const assignableToCheck: Expectation = result;
2266+
const assignablefromCheck: typeof result = assignableToCheck;
2267+
});
2268+
22492269
(function describe(_ = 'inference on large tuples') {
22502270
(function it(_ = 'Should correctly infer the type on tuples with 6 elements') {
22512271
type Input = CreateTuple<6, ResultAsync<string, number>>
2252-
type Expectation = ResultAsync<CreateTuple<6, string>, CreateTuple<6, number>>
2272+
type Expectation = ResultAsync<CreateTuple<6, string>, number[]>
22532273

22542274
const inputValues = input<Input>()
22552275
const result = ResultAsync.combineWithAllErrors(inputValues)
@@ -2261,7 +2281,7 @@ type CreateTuple<L, V = string> =
22612281

22622282
(function it(_ = 'Should correctly infer the type on tuples with 15 elements') {
22632283
type Input = CreateTuple<15, ResultAsync<string, number>>
2264-
type Expectation = ResultAsync<CreateTuple<15, string>, CreateTuple<15, number>>
2284+
type Expectation = ResultAsync<CreateTuple<15, string>, number[]>
22652285

22662286
const inputValues = input<Input>()
22672287
const result = ResultAsync.combineWithAllErrors(inputValues)
@@ -2273,7 +2293,7 @@ type CreateTuple<L, V = string> =
22732293

22742294
(function it(_ = 'Should correctly infer the type on tuples with 30 elements') {
22752295
type Input = CreateTuple<30, ResultAsync<string, number>>
2276-
type Expectation = ResultAsync<CreateTuple<30, string>, CreateTuple<30, number>>
2296+
type Expectation = ResultAsync<CreateTuple<30, string>, number[]>
22772297

22782298
const inputValues = input<Input>()
22792299
const result = ResultAsync.combineWithAllErrors(inputValues)
@@ -2285,7 +2305,7 @@ type CreateTuple<L, V = string> =
22852305

22862306
(function it(_ = 'Should correctly infer the type on tuples with 49 elements') {
22872307
type Input = CreateTuple<49 , ResultAsync<string, number>>
2288-
type Expectation = ResultAsync<CreateTuple<49, string>, CreateTuple<49, number>>
2308+
type Expectation = ResultAsync<CreateTuple<49, string>, number[]>
22892309

22902310
const inputValues = input<Input>()
22912311
const result = ResultAsync.combineWithAllErrors(inputValues)

0 commit comments

Comments
 (0)