A result/option utility for typescript.
import { Result, Ok, Err } from "yarut";
const apiCall = (): Result<string, Error> => {
return ResultUtil.Ok("All good!");
}
const result = apiCall();
// With help of these type guards, either `value` or `error` is available
if (result.isOk()) {
console.log("It works!", result.value);
} else {
console.log("Oh no :(", result.error);
}
Result
- Encode errors as type information and explicitly handle themOption
- Don't use null/undefined to express values that doesn't exist. UseNone
andSome
to indicate it.
import {optionSuperRegisterRecipe, resultSuperRegisterRecipe } from "yarut";
import superjson from "superjson";
superjson.registerCustom(resultSuperRegisterRecipe(superjson), "yarut/Result");
superjson.registerCustom(optionSuperRegisterRecipe(superjson), "yarut/Option");
Inspired by the work of @swan-io/boxed