Result<T, E>
A result monad that can either be a success or an error.
The type of the success value
The type of the error value
Result<T, E>
ResultOk<T> | ResultErr<E>
A discriminated union type that is either a success or an error
Example
import { type Result, ok, err } from "@temelj/result";
function divide(a: number, b: number): Result<number, string> {
if (b === 0) {
return err("Division by zero");
}
return ok(a / b);
}
const result = divide(10, 2);
if (result.kind === "ok") {
console.log(result.value); // 5
}
ResultOk<T>
A result value that is a success.
The type of the success value
Discriminant property that identifies this as a success result
Example
import { type ResultOk } from "@temelj/result";
const success: ResultOk<number> = {
kind: "ok",
value: 42,
};
ResultErr<E>
A result value that is an error.
The type of the error value
Discriminant property that identifies this as an error result
Example
import { type ResultErr } from "@temelj/result";
const error: ResultErr<string> = {
kind: "error",
error: "Something went wrong",
};