Result pattern for typescript projects
Result Pattern was designed to permit failures and errors to be handled by the method call, as well as provide information to the caller about the result of its computation. This package was created to provide a simple implementation of this pattern for TypeScript and JavaScript.
Results.
typescript
function caller() : void {
try {
const value = callMe();
if (value == null) {
// handle the case where an invalid value was returned
return; // we don't want to do more processing
}
// do work with value
} catch (err) {
// handle the error that was thrown from the computation as relevant
}
}
function callMe() : number | null {
// this method may throw an error
const value = someComputation();
if (isValid(value)) {
return value;
}
// value is not valid, so return null as default to indicate failure
return null;
}
`
With the use of Result objects, we can make the implementation of the caller much more straightforward and clear about exactly how it is operating using the result of callMe.
`typescript
function caller() : void {
callMe()
.match((value) => {
// do work with the value
}, (theResult) => {
// handle the case where an invalid value was returned
},(theResult) => {
// handle the error that was thrown from the computation as relevant
}
);
}
function callMe() : ValueResult {
try {
// this method may throw an error
const value = someComputation(); // returns number
if (isValid(value)) {
return ValueResult.Success(value);
}
// value is not valid, so return a failure instance
return ValueResult.Failed("The computed value is invalid");
} catch (err) {
// we can now encapsulate the error in this method, so that it is
// not propagated up to parent methods
return ValueResult.Error(err);
}
}
`
Features
- Simple framework that provides both void and value Result implementations
- Suitable for both JavaScript and TypeScript
Installation
`typescript
npm install @kwyjibo-developments/typescript-result
`
Usage
`typescript
import { ValueResult } from '@kwyjibo-developments/typescript-result';
function isSystemStateValid() : ValueResult {
try {
// we don't control this function
// we know it returns null if it fails, or a boolean if it
// completes successfully
const result = someSystemStateIsValid();
if (result == null) {
return ValueResult.Failed("The validation check was unable to be completed.");
}
return ValueResult.Success(result);
} catch (err) {
return ValueResult.Error(err, "An unexpected error when performing the validation.");
}
}
`
In the above example, it is noted here that Success and Failed do not correspond to the state of the system being measured in the someSystemStateIsValid() function. Rather, they correspond to the ability to execute the function successfully and the type of result that it returns. The outputs of this function are shown below, along with their corresponding explanations.
- ValueResult Success with value true: The function someSystemStateIsValid completed without error and the current system state is valid.
- ValueResult Success with value false: The function someSystemStateIsValid completed without error and the current system state is not valid.
- ValueResult Failed: The function someSystemStateIsValid did not complete successfully. It failed with an error expected by the author of the function and returned without throwing an Error.
- ValueResult Error: The function someSystemStateIsValid did not complete successfully. It failed with an error not expected by the author of the function and returned by throwing an Error.
The match extension method is available to provide a fluent interface for chaining the outputs of a result with future actions to perform.
`typescript
import {Result, ValueResult} from '@kwyjibo-developments/typescript-result';
function doSomething() : Result { } // implementation omitted
function doSomethingElse() : ValueResult { } // implementation omitted
function handler() : void {
doSomething() // Line 1
.match(
doSomethingElse,
failureCallback,
errorCallback
)
.match(
completeCallback,
someOtherFailureCallback,
someOtherErrorCallback
);
}
``