Skip to content

Instantly share code, notes, and snippets.

@albertms10
Last active November 28, 2023 10:59
Show Gist options
  • Save albertms10/530670e632983bd9dc4e046f5f5628e3 to your computer and use it in GitHub Desktop.
Save albertms10/530670e632983bd9dc4e046f5f5628e3 to your computer and use it in GitHub Desktop.
TypeScript type to await an array of Results
type ResultSuccess<T> = { success: true; value: T };
type ResultFailure<T> = { success: false; error: T };
type ExtractFailure<T> = T extends ResultFailure<infer U> ? U : never;
type ExtractSuccess<T> = T extends ResultSuccess<infer U> ? U : never;
/**
* Represents the awaited results of an array of promises, each of which can resolve
* to either `ResultSuccess` or `ResultFailure`.
*
* This type resolves to a `Promise` that either:
* - Contains a `ResultSuccess<T>` object with a tuple reflecting the successful results
* of each promise in `T`. If a promise in the array does not resolve to `ResultSuccess`,
* its corresponding type in the tuple will be `never`.
* - Or, if any of the promises in the array results in a `ResultFailure`, a union type
* of the error types from the failures is added.
*
* @template T An array of promises, each resolving to either `ResultSuccess` or `ResultFailure`.
* @see https://gist.github.com/albertms10/530670e632983bd9dc4e046f5f5628e3
*
* @example
* // If all promises result in success:
* type Example1 = AwaitedResults<[Promise<ResultSuccess<string>>, Promise<ResultSuccess<number>>]>;
* // Promise<ResultSuccess<[string, number]>>
*
* @example
* // If any promise results in failure:
* type Example2 = AwaitedResults<[Promise<ResultSuccess<string> | ResultFailure<number>>, Promise<ResultFailure<boolean>>]>;
* // Promise<ResultSuccess<[string, never]> | ResultFailure<number | boolean>>
*/
export type AwaitedResults<T extends readonly any[]> = Promise<
| ResultSuccess<{
[K in keyof T]: ExtractSuccess<Awaited<T[K]>> extends never
? never
: ExtractSuccess<Awaited<T[K]>>;
}>
| (ExtractFailure<Awaited<T[number]>> extends never
? never
: ResultFailure<ExtractFailure<Awaited<T[number]>>>)
>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment