@@ -29,6 +29,20 @@ export type ResultValueOf<T> = T extends Result<infer TResultValue>
2929 ? TResultValue
3030 : unknown ;
3131
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+
3246/**
3347 * Represents a successful or failed operation
3448 */
@@ -275,11 +289,33 @@ export class Result<TValue = Unit, TError = string> {
275289 return ! this . isSuccess ;
276290 }
277291
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 > {
279312 return this . isSuccess ;
280313 }
281314
282- hasError ( ) : this is Result < TValue > & { error : TError } {
315+ /**
316+ * Checks if result operation failed.
317+ */
318+ hasError ( ) : this is ResultFailure < TValue , TError > {
283319 return ! this . isSuccess ;
284320 }
285321
@@ -291,13 +327,6 @@ export class Result<TValue = Unit, TError = string> {
291327 error : undefined ,
292328 } ;
293329
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-
301330 /**
302331 * Creates a new Result instance in a guaranteed valid state
303332 * @param {{ value?: TValue, error?: TError, isSuccess: boolean } } state the initial state of the Result
0 commit comments