Created
August 12, 2021 07:41
-
-
Save AKST/a110eb97229c83adc47c2b95d252c783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// feel free to use as you please, btw. | |
type T<V, E> = | |
| { t: 'ok', v: V } | |
| { t: 'err', e: E } | |
type UnwrapResultErrors<R> = | |
R extends [T<any, infer E>, ...(infer Tail)] | |
? (E | UnwrapResultErrors<Tail>) : | |
R extends [T<any, infer E>] | |
? E : | |
never; | |
type UnwrapResultOks<R> = | |
R extends [T<infer V, any>, ...(infer Tail)] | |
? [V, ...UnwrapResultOks<Tail>] : | |
R extends [T<infer V, any>] | |
? [V] : | |
[]; | |
type AllifyResult2Impl<I, O> = | |
O extends T<[...infer VO], infer EO> ? | |
I extends [T<infer VI, infer EI>, ...(infer Rest)] | |
? AllifyResult2Impl<Rest, T<[...VO, VI], EO | EI>> : | |
I extends [T<infer VI, infer EI>] | |
? T<[...VO, VI], EO | EI> : | |
never : | |
never; | |
type AllifyResult1<R> = T<UnwrapResultOks<R>, UnwrapResultErrors<R>>; | |
type AllifyResult2<R> = AllifyResult2Impl<R, T<[], never>>; | |
export function all1<R extends [...any[]]>(results: [...R]): AllifyResult1<R> { | |
const output: any[] = [] | |
for (const it of results as any) { | |
if (!it.ok) return it; | |
output.push(it.value); | |
} | |
return { t: 'ok', v: output } as any; | |
} | |
export function all2<R extends [...any[]]>(results: [...R]): AllifyResult2<R> { | |
const output: any[] = [] | |
for (const it of results as any) { | |
if (!it.ok) return it; | |
output.push(it.value); | |
} | |
return { t: 'ok', v: output } as any; | |
} | |
// this works... (using AllifyResult1) | |
const a: AllifyResult1<[T<number, undefined>, T<string, undefined>]> = { t: 'ok', v: [1, '' ] }; | |
const b: AllifyResult1<[T<number, undefined>, T<string, undefined>]> = { t: 'err', e: undefined }; | |
// this does not work... (using AllifyResult2) | |
const c: AllifyResult2<[T<number, undefined>, T<string, undefined>]> = { t: 'ok', v: [1, '' ] }; | |
const d: AllifyResult2<[T<number, undefined>, T<string, undefined>]> = { t: 'err', e: undefined }; | |
// this does not work... (using AllifyResult1) | |
const e: T<[number, string], undefined> = all1([{ t: 'ok', v: 1 }, { t: 'ok', v: '' }]); | |
const f: T<[number, string], undefined> = all1([{ t: 'ok', v: 1 }, { t: 'err', e: undefined }]); | |
// this works... (using AllifyResult2) | |
const g: T<[number, string], undefined> = all2([{ t: 'ok', v: 1 }, { t: 'ok', v: '' }]); | |
const h: T<[number, string], undefined> = all2([{ t: 'ok', v: 1 }, { t: 'err', e: undefined }]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2021 Angus Thomsen | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment