@@ -23,12 +23,20 @@ import {
2323} from './utilities.js' ;
2424
2525/**
26- * Allows to extract the Value of the given Result-Type
27- * e.g. ResultValueOf<Result<string>> => string
26+ * Allows to extract the Value of the given `Result` or `ResultAsync`
27+ *
28+ * @example ResultValue<Result<string>> => string
29+ * @example ResultValue<ResultAsync<string>> => string
2830 */
29- export type ResultValueOf < T > = T extends Result < infer TResultValue >
31+ export type ResultValue < T > = T extends Result < infer TResultValue >
3032 ? TResultValue
31- : unknown ;
33+ : T extends ResultAsync < infer TResultAsyncValue >
34+ ? TResultAsyncValue
35+ : never ;
36+
37+ export type ResultRecord < TResultRecord > = {
38+ [ K in keyof TResultRecord ] : ResultValue < TResultRecord [ K ] > ;
39+ } ;
3240
3341/**
3442 * Represents a successful Result operation.
@@ -55,9 +63,9 @@ export class Result<TValue = Unit, TError = string> {
5563 * @param results The Results to be combined.
5664 * @returns A Result that is a success when all the input results are also successes.
5765 */
58- static combine < T extends Record < string , Result < unknown > > > (
59- results : T
60- ) : Result < { [ K in keyof T ] : ResultValueOf < T [ K ] > } > {
66+ static combine < TResultRecord extends Record < string , Result < unknown > > > (
67+ results : TResultRecord
68+ ) : Result < ResultRecord < TResultRecord > > {
6169 const resultEntries = Object . entries ( results ) ;
6270
6371 const failedResults = resultEntries . filter (
@@ -74,7 +82,9 @@ export class Result<TValue = Unit, TError = string> {
7482 } , { } as { [ key : string ] : unknown } ) ;
7583
7684 return Result . success (
77- values as Some < { [ K in keyof T ] : ResultValueOf < T [ K ] > } >
85+ values as Some < {
86+ [ K in keyof TResultRecord ] : ResultValue < TResultRecord [ K ] > ;
87+ } >
7888 ) ;
7989 }
8090
@@ -85,6 +95,12 @@ export class Result<TValue = Unit, TError = string> {
8595 return Result . failure ( errorMessages ) ;
8696 }
8797
98+ static combineInOrderAsync <
99+ TResultRecord extends Record < string , Result < unknown > | ResultAsync < unknown > >
100+ > ( record : TResultRecord ) : ResultAsync < ResultRecord < TResultRecord > > {
101+ return ResultAsync . combineInOrder ( record ) ;
102+ }
103+
88104 /**
89105 * Creates a new successful Result with a string error type
90106 * and Unit value type
@@ -692,7 +708,7 @@ export class Result<TValue = Unit, TError = string> {
692708 /**
693709 * Maps the value successful Result to a new async value wrapped in a ResultAsync
694710 * @param projection a function given the value of the current Result which returns a Promise of some value
695-
711+
696712 * @returns
697713 */
698714 mapAsync < TNewValue > (
@@ -706,7 +722,7 @@ export class Result<TValue = Unit, TError = string> {
706722 /**
707723 * Maps the error of a failed Result to a new async value wrapped in a ResultAsync
708724 * @param projection a function given the error of the current Result which returns a Promise of some value
709-
725+
710726 * @returns
711727 */
712728 mapFailureAsync (
@@ -733,7 +749,7 @@ export class Result<TValue = Unit, TError = string> {
733749 /**
734750 * Maps a successful Result to a new ResultAsync
735751 * @param projection
736-
752+
737753 */
738754 bindAsync < TNewValue > (
739755 projection : FunctionOfTtoK < TValue , Promise < Result < TNewValue , TError > > >
@@ -748,7 +764,7 @@ export class Result<TValue = Unit, TError = string> {
748764 /**
749765 * Maps a successful Result to a new ResultAsync
750766 * @param projection
751-
767+
752768 * @returns
753769 */
754770 bindAsync < TNewValue > (
@@ -856,7 +872,7 @@ export class Result<TValue = Unit, TError = string> {
856872 /**
857873 * Executes an async action if the Result succeeded
858874 * @param action a function given the Result's value returns a Promise
859-
875+
860876 * @returns a ResultAsync
861877 */
862878 tapAsync ( action : AsyncActionOfT < TValue > ) : ResultAsync < TValue , TError > {
0 commit comments