Result Namespace
Signature
export declare namespace Result
Interfaces
Interface | Description |
---|---|
Error | Wraps an error of type TError . |
Ok | Wraps a result of type TOk . |
Enumerations
Enum | Description |
---|---|
ResultType | Tag value use to differentiate the members of the Result discriminated union. |
Functions
Function | Return Type | Description |
---|---|---|
error(error) | Error<TError> | Factory function for making a unsuccessful Result. |
isError(result) | result is Error<TError> | Type guard for unsuccessful Result. |
isOk(result) | result is Ok<TOk> | Type guard for successful Result. |
mapError(result, map) | Result<TOk, TErrorOut> | Maps the given result with the given function when the result is an error. |
mapOk(result, map) | Result<TOkOut, TError> | Maps the given result with the given function when the result is ok. |
ok(result) | Ok<TOk> | Factory function for making a successful Result. |
Function Details
error
Factory function for making a unsuccessful Result.
Signature
function error<TError>(error: TError): Error<TError>;
Type Parameters
Parameter | Description |
---|---|
TError |
Parameters
Parameter | Type | Description |
---|---|---|
error | TError | The error to wrap in the Result. |
Returns
Return type: Error<TError>
isError
Type guard for unsuccessful Result.
Signature
function isError<TOk, TError>(result: Result<TOk, TError>): result is Error<TError>;
Type Parameters
Parameter | Description |
---|---|
TOk | |
TError |
Parameters
Parameter | Type | Description |
---|---|---|
result | Result<TOk, TError> |
Returns
True if result
is unsuccessful.
Return type: result is Error<TError>
isOk
Type guard for successful Result.
Signature
function isOk<TOk, TError>(result: Result<TOk, TError>): result is Ok<TOk>;
Type Parameters
Parameter | Description |
---|---|
TOk | |
TError |
Parameters
Parameter | Type | Description |
---|---|---|
result | Result<TOk, TError> |
Returns
True if result
is successful.
Return type: result is Ok<TOk>
mapError
Maps the given result with the given function when the result is an error.
Signature
function mapError<TOk, TErrorIn, TErrorOut>(result: Result<TOk, TErrorIn>, map: (error: TErrorIn) => TErrorOut): Result<TOk, TErrorOut>;
Type Parameters
Parameter | Description |
---|---|
TOk | |
TErrorIn | |
TErrorOut |
Parameters
Parameter | Type | Description |
---|---|---|
result | Result<TOk, TErrorIn> | The result to map. |
map | (error: TErrorIn) => TErrorOut | The function to apply to derive the new error. |
Returns
The given result if it is ok, the mapped result otherwise.
Return type: Result<TOk, TErrorOut>
mapOk
Maps the given result with the given function when the result is ok.
Signature
function mapOk<TOkIn, TOkOut, TError>(result: Result<TOkIn, TError>, map: (ok: TOkIn) => TOkOut): Result<TOkOut, TError>;
Type Parameters
Parameter | Description |
---|---|
TOkIn | |
TOkOut | |
TError |
Parameters
Parameter | Type | Description |
---|---|---|
result | Result<TOkIn, TError> | The result to map. |
map | (ok: TOkIn) => TOkOut | The function to apply to derive the new result. |
Returns
The given result if it is not ok, the mapped result otherwise.
Return type: Result<TOkOut, TError>
ok
Factory function for making a successful Result.
Signature
function ok<TOk>(result: TOk): Ok<TOk>;
Type Parameters
Parameter | Description |
---|---|
TOk |
Parameters
Parameter | Type | Description |
---|---|---|
result | TOk | The result to wrap in the Result. |
Returns
Return type: Ok<TOk>