Creates a success Result.
The success value to wrap
A success Result containing the value
Example
import { ok } from "@temelj/result";
const result = ok(42);
console.log(result); // { kind: "ok", value: 42 }
err
Creates an error Result.
An error Result containing the error
Example
import { err } from "@temelj/result";
const result = err("Invalid input");
console.log(result); // { kind: "error", error: "Invalid input" }
isOk
Type guard that checks if a Result is a success.
True if the Result is a success, false otherwise
Example
import { ok, isOk } from "@temelj/result";
const result = ok(42);
if (isOk(result)) {
console.log(result.value); // TypeScript knows result.value exists
}
isErr
Type guard that checks if a Result is an error.
True if the Result is an error, false otherwise
Example
import { err, isErr } from "@temelj/result";
const result = err("Failed");
if (isErr(result)) {
console.log(result.error); // TypeScript knows result.error exists
}
unwrap
Extracts the success value from a Result or throws the error.
The success value if the Result is Ok
Throws the error value if the Result is Err
Example
import { ok, err, unwrap } from "@temelj/result";
const success = ok(42);
console.log(unwrap(success)); // 42
const failure = err("Invalid");
unwrap(failure); // Throws "Invalid"
unwrapErr
Extracts the error value from a Result or throws an error.
The error value if the Result is Err
Throws an Error if the Result is Ok
Example
import { err, unwrapErr } from "@temelj/result";
const failure = err("Invalid input");
console.log(unwrapErr(failure)); // "Invalid input"
unwrapOr
Extracts the success value from a Result or returns a default value.
The default value to return if the Result is an error. Can be a value or a function that returns a value.
The success value if Ok, otherwise the default value
Example
import { ok, err, unwrapOr } from "@temelj/result";
const success = ok(42);
console.log(unwrapOr(success, 0)); // 42
const failure = err("Invalid");
console.log(unwrapOr(failure, 0)); // 0
// With a function
console.log(unwrapOr(failure, () => Math.random())); // Random number
map
Maps the success value of a Result to a new value.
Function to transform the success value
A new Result with the transformed value, or the original error
Example
import { ok, err, map } from "@temelj/result";
const result = ok(5);
const doubled = map(result, (x) => x * 2);
console.log(doubled); // { kind: "ok", value: 10 }
const error = err("Invalid");
const mapped = map(error, (x) => x * 2);
console.log(mapped); // { kind: "error", error: "Invalid" }
mapErr
Maps the error value of a Result to a new error value.
Function to transform the error value
A new Result with the transformed error, or the original success value
Example
import { err, mapErr } from "@temelj/result";
const result = err("failed");
const mapped = mapErr(result, (e) => e.toUpperCase());
console.log(mapped); // { kind: "error", error: "FAILED" }
fromThrowable
Calls a function that may throw and returns a Result.
The synchronous function to execute
Optional function to convert the unknown thrown value to type E
A Result containing either the function’s return value or the caught error
Example
import { fromThrowable } from "@temelj/result";
const result = fromThrowable(() => {
return JSON.parse("{invalid json}");
});
const withErrorMapping = fromThrowable(
() => JSON.parse("{invalid}"),
(e) => `Parse error: ${e}`
);
fromPromise
Calls a function that returns a Promise and returns a Promise<Result>.
The async function to execute
Optional function to convert the unknown rejection reason to type E
return
Promise<Result<T, E | unknown>>
A Promise of a Result containing either the resolved value or the caught error
Example
import { fromPromise } from "@temelj/result";
const result = await fromPromise(async () => {
const response = await fetch("/api/data");
return response.json();
});
const withErrorMapping = await fromPromise(
async () => fetch("/api/data").then((r) => r.json()),
(e) => `Network error: ${e}`
);