@@ -169,52 +169,66 @@ export const equals =
169169 return either . equals ( other ) ;
170170 } ;
171171
172- export const all = < L , R > ( eithers : Either < L , R > [ ] ) : Either < L [ ] , R [ ] > => {
173- const values : R [ ] = [ ] ;
174- const errors : L [ ] = [ ] ;
172+ type UnwrapEitherArray < T extends Either < unknown , unknown > [ ] > = {
173+ [ K in keyof T ] : T [ K ] extends Either < unknown , infer R > ? R : never ;
174+ } ;
175+
176+ type UnwrapLeftArray < T extends Either < unknown , unknown > [ ] > = {
177+ [ K in keyof T ] : T [ K ] extends Either < infer L , unknown > ? L : never ;
178+ } [ number ] ;
179+
180+ export function all < T extends Either < unknown , unknown > [ ] > (
181+ eithers : [ ...T ]
182+ ) : Either < UnwrapLeftArray < T > [ ] , UnwrapEitherArray < T > > {
183+ const values : UnwrapEitherArray < T > [ number ] [ ] = [ ] ;
184+ const errors : UnwrapLeftArray < T > [ ] = [ ] ;
175185
176186 for ( const either of eithers ) {
177187 if ( either . isLeft ( ) ) {
178- errors . push ( either . extract ( ) as L ) ;
188+ errors . push ( either . extract ( ) as UnwrapLeftArray < T > ) ;
179189 } else {
180- values . push ( either . extract ( ) as R ) ;
190+ values . push ( either . extract ( ) as UnwrapEitherArray < T > [ number ] ) ;
181191 }
182192 }
183193
184194 if ( errors . length > 0 ) {
185- return new Left < L [ ] , R [ ] > ( errors ) ;
195+ return new Left ( errors ) as Either < UnwrapLeftArray < T > [ ] , UnwrapEitherArray < T > > ;
186196 }
187197
188- return new Right < L [ ] , R [ ] > ( values ) ;
189- } ;
198+ return new Right ( values ) as Either < UnwrapLeftArray < T > [ ] , UnwrapEitherArray < T > > ;
199+ }
190200
191- export const sequence = < L , R > ( eithers : Either < L , R > [ ] ) : Either < L , R [ ] > => {
192- const values : R [ ] = [ ] ;
201+ export function sequence < T extends Either < unknown , unknown > [ ] > (
202+ eithers : [ ...T ]
203+ ) : Either < UnwrapLeftArray < T > , UnwrapEitherArray < T > > {
204+ const values : UnwrapEitherArray < T > [ number ] [ ] = [ ] ;
193205
194206 for ( const either of eithers ) {
195207 if ( either . isLeft ( ) ) {
196- return new Left < L , R [ ] > ( either . extract ( ) as L ) ;
208+ return new Left ( either . extract ( ) as UnwrapLeftArray < T > ) as Either < UnwrapLeftArray < T > , UnwrapEitherArray < T > > ;
197209 }
198210
199- values . push ( either . extract ( ) as R ) ;
211+ values . push ( either . extract ( ) as UnwrapEitherArray < T > [ number ] ) ;
200212 }
201213
202- return new Right < L , R [ ] > ( values ) ;
203- } ;
214+ return new Right ( values ) as Either < UnwrapLeftArray < T > , UnwrapEitherArray < T > > ;
215+ }
204216
205- export const partition = < L , R > ( eithers : Either < L , R > [ ] ) : { lefts : L [ ] ; rights : R [ ] } => {
217+ export function partition < T extends Either < unknown , unknown > [ ] > (
218+ eithers : [ ...T ]
219+ ) : { lefts : UnwrapLeftArray < T > [ ] ; rights : UnwrapEitherArray < T > } {
206220 return eithers . reduce (
207221 ( acc , either ) => {
208222 if ( either . isLeft ( ) ) {
209- acc . lefts . push ( either . extract ( ) as L ) ;
223+ acc . lefts . push ( either . extract ( ) as UnwrapLeftArray < T > ) ;
210224 } else {
211- acc . rights . push ( either . extract ( ) as R ) ;
225+ acc . rights . push ( either . extract ( ) as UnwrapEitherArray < T > [ number ] ) ;
212226 }
213227 return acc ;
214228 } ,
215- { lefts : [ ] as L [ ] , rights : [ ] as R [ ] }
216- ) ;
217- } ;
229+ { lefts : [ ] as UnwrapLeftArray < T > [ ] , rights : [ ] as UnwrapEitherArray < T > [ number ] [ ] }
230+ ) as { lefts : UnwrapLeftArray < T > [ ] ; rights : UnwrapEitherArray < T > } ;
231+ }
218232
219233export const left = < L , R = never > ( value : L ) : Either < L , R > => new Left ( value ) ;
220234export const right = < L , R > ( value : R ) : Either < L , R > => new Right ( value ) ;
0 commit comments