@@ -29,6 +29,20 @@ export type ResultValueOf<T> = T extends Result<infer TResultValue>
29
29
? TResultValue
30
30
: unknown ;
31
31
32
+ /**
33
+ * Represents a successful Result operation.
34
+ */
35
+ type ResultSuccess < TValue > = Result < TValue > & {
36
+ value : TValue ;
37
+ } ;
38
+
39
+ /**
40
+ * Represents a failed Result operation.
41
+ */
42
+ type ResultFailure < TValue , TError > = Result < TValue > & {
43
+ error : TError ;
44
+ } ;
45
+
32
46
/**
33
47
* Represents a successful or failed operation
34
48
*/
@@ -275,11 +289,33 @@ export class Result<TValue = Unit, TError = string> {
275
289
return ! this . isSuccess ;
276
290
}
277
291
278
- hasValue ( ) : this is Result < TValue > & { value : TValue } {
292
+ /**
293
+ * Yields value if the result operation succeeded.
294
+ * Hint: Use hasValue() upfront to be sure that result operation succeeded.
295
+ */
296
+ protected get value ( ) {
297
+ return this . state . value ;
298
+ }
299
+
300
+ /**
301
+ * Yields error if the result operation failed.
302
+ * Hint: Use hasError() upfront to be sure that result operation failed.
303
+ */
304
+ protected get error ( ) {
305
+ return this . state . error ;
306
+ }
307
+
308
+ /**
309
+ * Checks if result operation succeeded.
310
+ */
311
+ hasValue ( ) : this is ResultSuccess < TValue > {
279
312
return this . isSuccess ;
280
313
}
281
314
282
- hasError ( ) : this is Result < TValue > & { error : TError } {
315
+ /**
316
+ * Checks if result operation failed.
317
+ */
318
+ hasError ( ) : this is ResultFailure < TValue , TError > {
283
319
return ! this . isSuccess ;
284
320
}
285
321
@@ -291,13 +327,6 @@ export class Result<TValue = Unit, TError = string> {
291
327
error : undefined ,
292
328
} ;
293
329
294
- protected get value ( ) : TValue {
295
- return this . state . value as TValue ;
296
- }
297
- protected get error ( ) : TError {
298
- return this . state . error as TError ;
299
- }
300
-
301
330
/**
302
331
* Creates a new Result instance in a guaranteed valid state
303
332
* @param {{ value?: TValue, error?: TError, isSuccess: boolean } } state the initial state of the Result
0 commit comments