1
+ /// <reference path="../shared/index.ts" />
2
+
1
3
import type { BlobType , LiteralUnion } from '../shared'
2
4
3
5
import type { resultTags } from './tag'
6
+ import type { ResultAsync } from './utils/async'
4
7
import type { Err } from './utils/err'
5
8
import type { Ok } from './utils/ok'
6
9
import type { Tags } from './utils/tag'
@@ -37,7 +40,11 @@ declare global {
37
40
*/
38
41
type ErrorValues = LiteralUnion < Std . ExtractErrors < Std . Error [ keyof Std . Error ] > , `?${string } `>
39
42
40
- type InferOkType < R > = R extends Std . Result < infer T , BlobType , BlobType [ ] > ? T : never
43
+ type InferOkType < R > = R extends Std . Result < infer T , BlobType , BlobType [ ] >
44
+ ? T
45
+ : R extends ResultAsync < infer T , BlobType >
46
+ ? T
47
+ : never
41
48
type InferNameType < R > = R extends Err < BlobType , infer N , BlobType [ ] > ? N : never
42
49
type InferCauseType < R > = R extends Err < BlobType , BlobType , infer C > ? C : [ ]
43
50
@@ -75,8 +82,17 @@ declare global {
75
82
| Ok < T , N , C >
76
83
| Err < T , N , C >
77
84
85
+ type ExtractFromNested < U > = U extends PromiseLike < BlobType >
86
+ ? Std . InferOkType < Awaited < U > > extends never
87
+ ? Std . ExtractFromNested < Awaited < U > >
88
+ : Std . ExtractFromNested < Std . InferOkType < Awaited < U > > >
89
+ : U extends Ok < infer T >
90
+ ? Std . ExtractFromNested < T >
91
+ : U
92
+
78
93
type UnionsToResult <
79
- U ,
94
+ R ,
95
+ U = R extends PromiseLike < BlobType > ? Std . ExtractFromNested < R > : R ,
80
96
N = Exclude < U , Ok < BlobType , BlobType , BlobType [ ] > | Err < BlobType , BlobType , BlobType > > ,
81
97
O = Extract <
82
98
U | ( N extends never ? never : Ok < N , never , never > ) ,
@@ -85,7 +101,9 @@ declare global {
85
101
E = Extract < U , Err < BlobType , BlobType , BlobType > > ,
86
102
> = O | E extends never
87
103
? never
88
- : Std . Result < Std . InferOkType < O > , Std . InferNameType < E > , Std . InferCauseType < E > >
104
+ : R extends PromiseLike < BlobType >
105
+ ? ResultAsync < Std . InferOkType < O > , Std . InferNameType < E > , Std . InferCauseType < E > >
106
+ : Std . Result < Std . InferOkType < O > , Std . InferNameType < E > , Std . InferCauseType < E > >
89
107
90
108
type InjectError <
91
109
U ,
@@ -97,6 +115,12 @@ declare global {
97
115
Un | N ,
98
116
Uc [ number ] | C [ number ] extends never ? [ ] : ( Uc [ number ] | C [ number ] ) [ ]
99
117
>
100
- : never
118
+ : U extends ResultAsync < infer O , infer Un , infer Uc >
119
+ ? ResultAsync <
120
+ O ,
121
+ Un | N ,
122
+ Uc [ number ] | C [ number ] extends never ? [ ] : ( Uc [ number ] | C [ number ] ) [ ]
123
+ >
124
+ : never
101
125
}
102
126
}
0 commit comments