Class: Result<Value, Err>
Defined in: src/result.ts:777
Represents the outcome of an operation that can either succeed or fail.
Type Parameters
Value
Value
Err
Err
Constructors
Constructor
new Result<
Value
,Err
>(_value
,_error
):Result
<Value
,Err
>
Defined in: src/result.ts:778
Parameters
_value
Value
_error
Err
Returns
Result
<Value
, Err
>
Properties
$inferError
$inferError:
Err
Defined in: src/result.ts:793
Utility getter to infer the error type of the result. Note: this getter does not hold any value, it's only used for type inference.
$inferValue
$inferValue:
Value
Defined in: src/result.ts:787
Utility getter to infer the value type of the result. Note: this getter does not hold any value, it's only used for type inference.
Accessors
error
Get Signature
get error():
ErrorOr
<Value
,Err
,undefined
>
Defined in: src/result.ts:861
Retrieves the encapsulated error of the result.
Examples
obtaining the value of a result, without checking if it's a failure
declare const result: Result<number, Error>;
result.error; // Error | undefined
obtaining the error of a result, after checking for failure
declare const result: Result<number, Error>;
if (!result.ok) {
result.error; // Error
}
Returns
ErrorOr
<Value
, Err
, undefined
>
The error if the operation failed, otherwise undefined
.
NOTE
You can use Result.ok
to narrow down the type to a failed result.
isResult
Get Signature
get isResult():
true
Defined in: src/result.ts:802
Utility getter that checks if the current instance is a Result
.
Returns
true
ok
Get Signature
get ok(): [
Err
] extends [never
] ?true
:false
Defined in: src/result.ts:873
Returns
[Err
] extends [never
] ? true
: false
value
Get Signature
get value():
ValueOr
<Value
,Err
,undefined
>
Defined in: src/result.ts:831
Retrieves the encapsulated value of the result.
Examples
obtaining the value of a result, without checking if it's successful
declare const result: Result<number, Error>;
result.value; // number | undefined
obtaining the value of a result, after checking for success
declare const result: Result<number, Error>;
if (result.ok) {
result.value; // number
}
Returns
ValueOr
<Value
, Err
, undefined
>
The value if the operation was successful, otherwise undefined
.
Note: You can use Result.ok
to narrow down the type to a successful result.
Methods
[iterator]()
[iterator]():
Generator
<{async
:false
;error
:Err
; },Value
,any
>
Defined in: src/result.ts:795
Returns
Generator
<{ async
: false
; error
: Err
; }, Value
, any
>
errorOrNull()
errorOrNull():
ErrorOr
<Value
,Err
,null
>
Defined in: src/result.ts:948
Returns
ErrorOr
<Value
, Err
, null
>
the encapsulated error if the result is a failure, otherwise null
.
fold()
fold<
This
,SuccessResult
,FailureResult
>(this
,onSuccess
,onFailure
):Contains
<SuccessResult
|FailureResult
,AnyPromise
,SuccessResult
|FailureResult
> extendstrue
?Promise
<Awaited
<SuccessResult
> |Awaited
<FailureResult
>> :SuccessResult
|FailureResult
Defined in: src/result.ts:1073
Returns the result of the onSuccess callback when the result represents success or the result of the onFailure callback when the result represents a failure.
NOTE
Any exceptions that might be thrown inside the callbacks are not caught, so it is your responsibility to handle these exceptions
Type Parameters
This
This
extends AnyResult
SuccessResult
SuccessResult
FailureResult
FailureResult
Parameters
this
This
onSuccess
(value
) => SuccessResult
callback function to run when the result is successful. The callback can be async as well.
onFailure
(error
) => FailureResult
callback function to run when the result is a failure. The callback can be async as well.
Returns
Contains
<SuccessResult
| FailureResult
, AnyPromise
, SuccessResult
| FailureResult
> extends true
? Promise
<Awaited
<SuccessResult
> | Awaited
<FailureResult
>> : SuccessResult
| FailureResult
the result of the callback that was executed.
Example
folding a result to a response-like object
declare const result: Result<User, NotFoundError | UserDeactivatedError>;
const response = result.fold(
(user) => ({ status: 200, body: user }),
(error) => {
switch (error.type) {
case "not-found":
return { status: 404, body: "User not found" };
case "user-deactivated":
return { status: 403, body: "User is deactivated" };
}
}
);
getOrDefault()
getOrDefault<
Else
>(defaultValue
):Value
|Else
Defined in: src/result.ts:982
Retrieves the value of the result, or a default value if the result is a failure.
Type Parameters
Else
Else
Parameters
defaultValue
Else
The value to return if the result is a failure.
Returns
Value
| Else
The encapsulated value if the result is successful, otherwise the default value.
Examples
obtaining the value of a result, or a default value
declare const result: Result<number, Error>;
const value = result.getOrDefault(0); // number
using a different type for the default value
declare const result: Result<number, Error>;
const value = result.getOrDefault("default"); // number | string
getOrElse()
getOrElse<
This
,Else
>(this
,onFailure
):Else
extendsPromise
<U
> ?Promise
<Value
|U
> :Value
|Else
Defined in: src/result.ts:1006
Retrieves the value of the result, or transforms the error using the onFailure callback into a value.
Type Parameters
This
This
extends AnyResult
Else
Else
Parameters
this
This
onFailure
(error
) => Else
callback function which allows you to transform the error into a value. The callback can be async as well.
Returns
Else
extends Promise
<U
> ? Promise
<Value
| U
> : Value
| Else
either the value if the result is successful, or the transformed error.
Examples
transforming the error into a value
declare const result: Result<number, Error>;
const value = result.getOrElse((error) => 0); // number
using an async callback
const value = await result.getOrElse(async (error) => 0); // Promise<number>
getOrNull()
getOrNull():
ValueOr
<Value
,Err
,null
>
Defined in: src/result.ts:955
Returns
ValueOr
<Value
, Err
, null
>
the encapsulated value if the result is successful, otherwise null
.
getOrThrow()
getOrThrow():
Value
Defined in: src/result.ts:1034
Retrieves the value of the result, or throws an error if the result is a failure.
Returns
Value
The value if the result is successful.
Throws
the encapsulated error if the result is a failure.
Example
obtaining the value of a result, or throwing an error
declare const result: Result<number, Error>;
const value = result.getOrThrow(); // number
isError()
isError():
this is [Err] extends [never] ? never : Error<Err>
Defined in: src/result.ts:913
Returns
this is [Err] extends [never] ? never : Error<Err>
true
if the result represents a failure, otherwise false
.
Deprecated
use Result.ok
instead. Type guard that checks whether the result is successful.
Example
checking if a result represents a failure
declare const result: Result<number, Error>;
if (result.isError()) {
result.error; // Error
}
isOk()
isOk():
this is [Value] extends [never] ? never : Ok<Value>
Defined in: src/result.ts:893
Returns
this is [Value] extends [never] ? never : Ok<Value>
true
if the result is successful, otherwise false
.
Deprecated
use Result.ok
instead. Type guard that checks whether the result is successful.
Example
checking if a result is successful
declare const result: Result<number, Error>;
if (result.isOk()) {
result.value; // number
}
map()
map<
This
,ReturnType
,U
>(this
,transform
): [InferValue
<This
>] extends [never
] ?Error
<InferError
<This
>> : [ReturnType
] extends [Generator
<unknown
,any
,any
> |AsyncGenerator
<unknown
,any
,any
>] ?IfGeneratorAsync
<ReturnType
<ReturnType
>,AsyncResult
<InferGeneratorReturn
<ReturnType
<ReturnType
>>,InferError
<This
> |InferGeneratorError
<ReturnType
<ReturnType
>>>,Result
<InferGeneratorReturn
<ReturnType
<ReturnType
>>,InferError
<This
> |InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ?PValue
extendsU
?AsyncResult
<ExtractValue
<U
>,InferError
<This
> |ExtractError
<U
>> :never
:IfReturnsAsync
<ReturnType
,ReturnType
extendsU
?AsyncResult
<ExtractValue
<U
>,InferError
<This
> |ExtractError
<U
>> :never
,ReturnType
extendsU
?Result
<ExtractValue
<U
>,InferError
<This
> |ExtractError
<U
>> :never
>
Defined in: src/result.ts:1296
Transforms the value of a successful result using the transform callback. The transform callback can also be a generator function or a function that returns other Result or AsyncResult instances, which will be returned as-is (the Error
types will be merged). Conceptually, it is similar to Array.flatMap
. This map operation will be ignored if the current result represents a failure.
Type Parameters
This
This
extends AnyResult
ReturnType
ReturnType
U
U
= Awaited
<ReturnType
>
Parameters
this
This
transform
(value
) => ReturnType
callback function to transform the value of the result. The callback can be async or a generator function as well.
Returns
[InferValue
<This
>] extends [never
] ? Error
<InferError
<This
>> : [ReturnType
] extends [Generator
<unknown
, any
, any
> | AsyncGenerator
<unknown
, any
, any
>] ? IfGeneratorAsync
<ReturnType
<ReturnType
>, AsyncResult
<InferGeneratorReturn
<ReturnType
<ReturnType
>>, InferError
<This
> | InferGeneratorError
<ReturnType
<ReturnType
>>>, Result
<InferGeneratorReturn
<ReturnType
<ReturnType
>>, InferError
<This
> | InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ? PValue
extends U
? AsyncResult
<ExtractValue
<U
>, InferError
<This
> | ExtractError
<U
>> : never
: IfReturnsAsync
<ReturnType
, ReturnType
extends U
? AsyncResult
<ExtractValue
<U
>, InferError
<This
> | ExtractError
<U
>> : never
, ReturnType
extends U
? Result
<ExtractValue
<U
>, InferError
<This
> | ExtractError
<U
>> : never
>
a new Result
instance with the transformed value, or a new AsyncResult
instance if the transform function is async.
NOTE
Any exceptions that might be thrown inside the transform callback are not caught, so it is your responsibility to handle these exceptions. Please refer to Result.mapCatching
for a version that catches exceptions and encapsulates them in a failed result.
Examples
transforming the value of a result
declare const result: Result<number, Error>;
const transformed = result.map((value) => value * 2); // Result<number, Error>
returning a result instance
declare const result: Result<number, Error>;
declare function multiplyByTwo(value: number): Result<number, Error>;
const transformed = result.map((value) => multiplyByTwo(value)); // Result<number, Error>
doing an async transformation
declare const result: Result<number, Error>;
const transformed = result.map(async (value) => value * 2); // AsyncResult<number, Error>
returning an async result instance
declare const result: Result<number, Error>;
declare function storeValue(value: number): AsyncResult<boolean, Error>;
const transformed = result.map((value) => storeValue(value)); // AsyncResult<boolean, Error>
using a generator function to transform the value
function* doubleValue(value: number) {
return value * 2;
}
declare const result: Result<number, Error>;
const transformed = result.map(doubleValue); // Result<number, Error>
mapCatching()
mapCatching<
This
,ReturnType
,ErrorType
,U
>(this
,transformValue
,transformError?
): [InferValue
<This
>] extends [never
] ?Error
<InferError
<This
>> : [ReturnType
] extends [Generator
<unknown
,any
,any
> |AsyncGenerator
<unknown
,any
,any
>] ?IfGeneratorAsync
<ReturnType
<ReturnType
>,AsyncResult
<InferGeneratorReturn
<ReturnType
<ReturnType
>>,ErrorType
|InferError
<This
> |InferGeneratorError
<ReturnType
<ReturnType
>>>,Result
<InferGeneratorReturn
<ReturnType
<ReturnType
>>,ErrorType
|InferError
<This
> |InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ?PValue
extendsU
?AsyncResult
<ExtractValue
<U
>,ErrorType
|InferError
<This
> |ExtractError
<U
>> :never
:IfReturnsAsync
<ReturnType
,ReturnType
extendsU
?AsyncResult
<ExtractValue
<U
>,ErrorType
|InferError
<This
> |ExtractError
<U
>> :never
,ReturnType
extendsU
?Result
<ExtractValue
<U
>,ErrorType
|InferError
<This
> |ExtractError
<U
>> :never
>
Defined in: src/result.ts:1357
Like Result.map
it transforms the value of a successful result using the transformValue callback. In addition, it catches any exceptions that might be thrown inside the transformValue callback and encapsulates them in a failed result.
Type Parameters
This
This
extends AnyResult
ReturnType
ReturnType
ErrorType
ErrorType
= Error
U
U
= Awaited
<ReturnType
>
Parameters
this
This
transformValue
(value
) => ReturnType
callback function to transform the value of the result. The callback can be async or a generator function as well.
transformError?
(err
) => ErrorType
callback function to transform any potential caught error while transforming the value.
Returns
[InferValue
<This
>] extends [never
] ? Error
<InferError
<This
>> : [ReturnType
] extends [Generator
<unknown
, any
, any
> | AsyncGenerator
<unknown
, any
, any
>] ? IfGeneratorAsync
<ReturnType
<ReturnType
>, AsyncResult
<InferGeneratorReturn
<ReturnType
<ReturnType
>>, ErrorType
| InferError
<This
> | InferGeneratorError
<ReturnType
<ReturnType
>>>, Result
<InferGeneratorReturn
<ReturnType
<ReturnType
>>, ErrorType
| InferError
<This
> | InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ? PValue
extends U
? AsyncResult
<ExtractValue
<U
>, ErrorType
| InferError
<This
> | ExtractError
<U
>> : never
: IfReturnsAsync
<ReturnType
, ReturnType
extends U
? AsyncResult
<ExtractValue
<U
>, ErrorType
| InferError
<This
> | ExtractError
<U
>> : never
, ReturnType
extends U
? Result
<ExtractValue
<U
>, ErrorType
| InferError
<This
> | ExtractError
<U
>> : never
>
a new Result
instance with the transformed value, or a new AsyncResult
instance if the transform function is async.
mapError()
mapError<
This
,NewError
>(this
,transform
):Result
<InferValue
<This
>,NewError
>
Defined in: src/result.ts:1430
Transforms the encapsulated error of a failed result using the transform callback into a new error. This can be useful for instance to capture similar or related errors and treat them as a single higher-level error type
Type Parameters
This
This
extends AnyResult
NewError
NewError
Parameters
this
This
transform
(error
) => NewError
callback function to transform the error of the result.
Returns
Result
<InferValue
<This
>, NewError
>
new Result
instance with the transformed error.
Example
transforming the error of a result
declare const result: Result<number, ErrorA>;
result.mapError((error) => new ErrorB(error.message)); // Result<number, ErrorB>
match()
match<
This
>(this
): [InferValue
<This
>] extends [never
] ?Matcher
<InferError
<This
>,never
> :"'match()' can only be called on a failed result. Please narrow the result by checking the 'ok' property."
Defined in: src/result.ts:1125
Allows you to effectively match the errors of a failed result using the returned instance of the Matcher class. Note: this method can only be called on a failed result, otherwise it will return undefined
. You can narrow the result by checking the ok
property.
Type Parameters
This
This
extends AnyResult
Parameters
this
This
Returns
[InferValue
<This
>] extends [never
] ? Matcher
<InferError
<This
>, never
> : "'match()' can only be called on a failed result. Please narrow the result by checking the 'ok' property."
Matcher instance that can be used to build a chain of matching patterns for the errors of the result.
Examples
Matching against error classes
declare const result: Result<number, NotFoundError | UserDeactivatedError>;
if (!result.ok) {
return result
.match()
.when(NotFoundError, (error) => console.error("User not found", error))
.when(UserDeactivatedError, (error) => console.error("User is deactivated", error))
.run()
}
Matching against string constants with an else clause
declare const result: Result<number, "not-found" | "user-deactivated">;
if (!result.ok) {
return result
.match()
.when("not-found", (error) => console.error("User not found", error))
.else((error) => console.error("Unknown error", error))
.run()
}
onFailure()
onFailure<
This
,ReturnValue
>(this
,action
):ReturnValue
extendsAnyPromise
?AsyncResult
<InferValue
<This
>,InferError
<This
>> :Result
<InferValue
<This
>,InferError
<This
>>
Defined in: src/result.ts:1154
Calls the action callback when the result represents a failure. It is meant to be used for side-effects and the operation does not modify the result itself.
Type Parameters
This
This
extends AnyResult
ReturnValue
ReturnValue
Parameters
this
This
action
(error
) => ReturnValue
callback function to run when the result is a failure. The callback can be async as well.
Returns
ReturnValue
extends AnyPromise
? AsyncResult
<InferValue
<This
>, InferError
<This
>> : Result
<InferValue
<This
>, InferError
<This
>>
the original instance of the result.
NOTE
Any exceptions that might be thrown inside the action callback are not caught, so it is your responsibility to handle these exceptions
Example
adding logging between operations
declare const result: Result<number, Error>;
result
.onFailure((error) => console.error("I'm failing!", error))
.map((value) => value * 2); // proceed with other operations
onSuccess()
Call Signature
onSuccess<
This
>(this
,action
):AsyncResult
<InferValue
<This
>,InferError
<This
>>
Defined in: src/result.ts:1207
Calls the action callback when the result represents a success. It is meant to be used for side-effects and the operation does not modify the result itself.
Type Parameters
This
This
extends AnyResult
Parameters
this
This
action
(value
) => Promise
<void
>
callback function to run when the result is successful. The callback can be async as well.
Returns
AsyncResult
<InferValue
<This
>, InferError
<This
>>
the original instance of the result. If the callback is async, it returns a new AsyncResult instance.
NOTE
Any exceptions that might be thrown inside the action callback are not caught, so it is your responsibility to handle these exceptions
Examples
adding logging between operations
declare const result: Result<number, Error>;
result
.onSuccess((value) => console.log("I'm a success!", value))
.map((value) => value * 2); // proceed with other operations
using an async callback
declare const result: Result<number, Error>;
const asyncResult = await result.onSuccess(async (value) => someAsyncOperation(value));
Call Signature
onSuccess<
This
>(this
,action
):Result
<InferValue
<This
>,InferError
<This
>>
Defined in: src/result.ts:1211
Calls the action callback when the result represents a success. It is meant to be used for side-effects and the operation does not modify the result itself.
Type Parameters
This
This
extends AnyResult
Parameters
this
This
action
(value
) => void
callback function to run when the result is successful. The callback can be async as well.
Returns
Result
<InferValue
<This
>, InferError
<This
>>
the original instance of the result. If the callback is async, it returns a new AsyncResult instance.
NOTE
Any exceptions that might be thrown inside the action callback are not caught, so it is your responsibility to handle these exceptions
Examples
adding logging between operations
declare const result: Result<number, Error>;
result
.onSuccess((value) => console.log("I'm a success!", value))
.map((value) => value * 2); // proceed with other operations
using an async callback
declare const result: Result<number, Error>;
const asyncResult = await result.onSuccess(async (value) => someAsyncOperation(value));
recover()
recover<
This
,ReturnType
,U
>(this
,onFailure
): [InferError
<This
>] extends [never
] ?Ok
<InferValue
<This
>> : [ReturnType
] extends [Generator
<unknown
,any
,any
> |AsyncGenerator
<unknown
,any
,any
>] ?IfGeneratorAsync
<ReturnType
<ReturnType
>,AsyncResult
<InferValue
<This
> |InferGeneratorReturn
<ReturnType
<ReturnType
>>,InferGeneratorError
<ReturnType
<ReturnType
>>>,Result
<InferValue
<This
> |InferGeneratorReturn
<ReturnType
<ReturnType
>>,InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ?PValue
extendsU
?AsyncResult
<InferValue
<This
> |ExtractValue
<U
>,ExtractError
<U
>> :never
:IfReturnsAsync
<ReturnType
,ReturnType
extendsU
?AsyncResult
<InferValue
<This
> |ExtractValue
<U
>,ExtractError
<U
>> :never
,ReturnType
extendsU
?Result
<InferValue
<This
> |ExtractValue
<U
>,ExtractError
<U
>> :never
>
Defined in: src/result.ts:1472
Transforms a failed result using the onFailure callback into a successful result. Useful for falling back to other scenarios when a previous operation fails. The onFailure callback can also be a generator function or a function that returns other Result or AsyncResult instances, which will be returned as-is (much like Array.flatMap). After a recovery, logically, the result can only be a success. Therefore, the error type is set to never
, unless the onFailure callback returns a result-instance with another error type.
Type Parameters
This
This
extends AnyResult
ReturnType
ReturnType
U
U
= Awaited
<ReturnType
>
Parameters
this
This
onFailure
(error
) => ReturnType
callback function to transform the error of the result. The callback can be async or a generator function as well.
Returns
[InferError
<This
>] extends [never
] ? Ok
<InferValue
<This
>> : [ReturnType
] extends [Generator
<unknown
, any
, any
> | AsyncGenerator
<unknown
, any
, any
>] ? IfGeneratorAsync
<ReturnType
<ReturnType
>, AsyncResult
<InferValue
<This
> | InferGeneratorReturn
<ReturnType
<ReturnType
>>, InferGeneratorError
<ReturnType
<ReturnType
>>>, Result
<InferValue
<This
> | InferGeneratorReturn
<ReturnType
<ReturnType
>>, InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ? PValue
extends U
? AsyncResult
<InferValue
<This
> | ExtractValue
<U
>, ExtractError
<U
>> : never
: IfReturnsAsync
<ReturnType
, ReturnType
extends U
? AsyncResult
<InferValue
<This
> | ExtractValue
<U
>, ExtractError
<U
>> : never
, ReturnType
extends U
? Result
<InferValue
<This
> | ExtractValue
<U
>, ExtractError
<U
>> : never
>
a new successful Result
instance or a new successful AsyncResult
instance when the result represents a failure, or the original instance if it represents a success.
NOTE
Any exceptions that might be thrown inside the onFailure callback are not caught, so it is your responsibility to handle these exceptions. Please refer to Result.recoverCatching
for a version that catches exceptions and encapsulates them in a failed result.
Example
transforming the error into a value Note: Since we recover after trying to persist in the database, we can assume that the DbError
has been taken care of and therefore it has been removed from the final result.
declare function persistInDB(item: Item): Result<Item, DbError>;
declare function persistLocally(item: Item): Result<Item, IOError>;
persistInDB(item).recover(() => persistLocally(item)); // Result<Item, IOError>
recoverCatching()
recoverCatching<
This
,ReturnType
,ErrorType
,U
>(this
,onFailure
,transformError?
): [InferError
<This
>] extends [never
] ?Ok
<InferValue
<This
>> : [ReturnType
] extends [Generator
<unknown
,any
,any
> |AsyncGenerator
<unknown
,any
,any
>] ?IfGeneratorAsync
<ReturnType
<ReturnType
>,AsyncResult
<InferValue
<This
> |InferGeneratorReturn
<ReturnType
<ReturnType
>>,ErrorType
|InferGeneratorError
<ReturnType
<ReturnType
>>>,Result
<InferValue
<This
> |InferGeneratorReturn
<ReturnType
<ReturnType
>>,ErrorType
|InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ?PValue
extendsU
?AsyncResult
<InferValue
<This
> |ExtractValue
<U
>,ErrorType
|ExtractError
<U
>> :never
:IfReturnsAsync
<ReturnType
,ReturnType
extendsU
?AsyncResult
<InferValue
<This
> |ExtractValue
<U
>,ErrorType
|ExtractError
<U
>> :never
,ReturnType
extendsU
?Result
<InferValue
<This
> |ExtractValue
<U
>,ErrorType
|ExtractError
<U
>> :never
>
Defined in: src/result.ts:1533
Like Result.recover
it transforms a failed result using the onFailure callback into a successful result. In addition, it catches any exceptions that might be thrown inside the onFailure callback and encapsulates them in a failed result.
Type Parameters
This
This
extends AnyResult
ReturnType
ReturnType
ErrorType
ErrorType
= Error
U
U
= Awaited
<ReturnType
>
Parameters
this
This
onFailure
(error
) => ReturnType
callback function to transform the error of the result. The callback can be async or a generator function as well.
transformError?
(err
) => ErrorType
callback function to transform any potential caught error while recovering the result.
Returns
[InferError
<This
>] extends [never
] ? Ok
<InferValue
<This
>> : [ReturnType
] extends [Generator
<unknown
, any
, any
> | AsyncGenerator
<unknown
, any
, any
>] ? IfGeneratorAsync
<ReturnType
<ReturnType
>, AsyncResult
<InferValue
<This
> | InferGeneratorReturn
<ReturnType
<ReturnType
>>, ErrorType
| InferGeneratorError
<ReturnType
<ReturnType
>>>, Result
<InferValue
<This
> | InferGeneratorReturn
<ReturnType
<ReturnType
>>, ErrorType
| InferGeneratorError
<ReturnType
<ReturnType
>>>> : [ReturnType
] extends [Promise
<PValue
>] ? PValue
extends U
? AsyncResult
<InferValue
<This
> | ExtractValue
<U
>, ErrorType
| ExtractError
<U
>> : never
: IfReturnsAsync
<ReturnType
, ReturnType
extends U
? AsyncResult
<InferValue
<This
> | ExtractValue
<U
>, ErrorType
| ExtractError
<U
>> : never
, ReturnType
extends U
? Result
<InferValue
<This
> | ExtractValue
<U
>, ErrorType
| ExtractError
<U
>> : never
>
a new successful Result
instance or a new successful AsyncResult
instance when the result represents a failure, or the original instance if it represents a success.
toString()
toString():
string
Defined in: src/result.ts:1599
Returns a string representation of the result.
Returns
string
toTuple()
toTuple<
T
,V
,E
>(this
): [E
] extends [never
] ? [V
,never
] : [V
] extends [never
] ? [never
,E
] : [V
,null
] | [null
,E
]
Defined in: src/result.ts:937
Type Parameters
T
T
extends AnyResult
V
V
= InferValue
<T
>
E
E
= InferError
<T
>
Parameters
this
T
Returns
[E
] extends [never
] ? [V
, never
] : [V
] extends [never
] ? [never
, E
] : [V
, null
] | [null
, E
]
the result in a tuple format where the first element is the value and the second element is the error. If the result is successful, the error will be null
. If the result is a failure, the value will be null
.
This method is especially useful when you want to destructure the result into a tuple and use TypeScript's narrowing capabilities.
Example
declare const result: Result<number, ErrorA>;
const [value, error] = result.toTuple();
if (error) {
// error is ErrorA
return;
}
// value must be a number