@reliverse/retyper: Handy utilities to supercharge TypeScript projects of any scale. Brings type-safe Result/Tuple patterns, crash-guards and micro-helpers to any TS runtime — inspired by Rust, Go and modern FP — with zero deps.
npm — Sponsor — Discord — DeepWiki — GitHub
retyper brings ergonomic, type-safe error handling and utility types to TypeScript, inspired by Rust, Go, and modern functional programming. It helps you write safer, more robust code with minimal boilerplate—ideal for any project where reliability and developer experience matter.
- 🧬 Result & Tuple patterns — safer async flows, no unhandled rejections
- 📦 Zero-bloat, runtime-safe — no dependencies, works everywhere TypeScript does
- 🛡️ Crash guards & exhaustiveness checks — never miss a case again
- 🧰 Handy helpers — for truncation, environment checks, and more
- 🎯 Drop-in, framework-agnostic — use in Node, Bun, Deno, browser, or library code
bun add @reliverse/retyper
# bun • pnpm • yarn • npmtype Ok<T> = { ok: true; data: T; error?: never };
type Err<E> = { ok: false; data?: never; error: E };
type Result<T, E = Error> = Ok<T> | Err<E>;tryCatch(val)— Wraps a promise or function, returns a ResulttryCatchSlim(promise)— Returns{ data, error }(nullables)tryTuple(val)— Returns[data, error]tupletryTupleErr(val)— Returns[error, data]tuple (Go style)safeify(fn)— Wraps a function to always return Resultunwrap(result)— Rust-style: get value or throwmust(promise)— Go-style: await or throwmap(result, fn)— Map value if okmapErr(result, fn)— Map error if errdevStrict(val)— Like tryCatch, but type-errors if you ignore the resultshouldNeverHappen(msg, ...args)— Crash guard for unreachable codecasesHandled(unexpectedCase)— Exhaustiveness check for unionsisDevEnv()— True ifNODE_ENV === "development"truncateString(msg, maxLength)— Truncate with ellipsis
import { tryCatch, unwrap, Result } from "@reliverse/retyper";
const res: Result<number> = tryCatch(() => parseInt("42"));
if (res.ok) {
console.log("Parsed:", res.data);
} else {
console.error("Failed:", res.error);
}import { tryCatch, must } from "@reliverse/retyper";
async function fetchJson(url: string) {
return must(fetch(url).then(r => r.json()));
}
// Or with tryCatchSlim:
import { tryCatchSlim } from "@reliverse/retyper";
const { data, error } = await tryCatchSlim(fetch("/api/data").then(r => r.json()));
if (error) return console.error(error);
console.log(data);import { tryTuple, tryTupleErr } from "@reliverse/retyper";
const [data, error] = await tryTuple(fetch("/api/data").then(r => r.json()));
if (error) return;
console.log(data);
const [err, val] = await tryTupleErr(Promise.resolve(123));
if (err) throw err;
console.log(val);import { safeify } from "@reliverse/retyper";
const safeParse = safeify(JSON.parse);
const res = safeParse("{\"foo\":42}");
if (res.ok) console.log(res.data.foo);import { casesHandled } from "@reliverse/retyper";
type Kind = "a" | "b";
function handle(k: Kind) {
if (k === "a") return 1;
if (k === "b") return 2;
return casesHandled(k); // Type error if not exhaustive
}import { shouldNeverHappen } from "@reliverse/retyper";
function foo(x: never) {
shouldNeverHappen("Unexpected value", x);
}- Type-safe error handling in any TypeScript project
- Safer async/await flows (no more unhandled promise rejections)
- Exhaustiveness checks for discriminated unions
- Utility helpers for robust, readable code
git clone https://github.com/reliverse/retyper
cd retyper
bun i
bun devCheck examples/ for more usage patterns.
try-catch.ts— Theo's video
type-fest— Advanced TS typesneverthrow— Result type for TypeScriptts-reset— TS' built-in typings, improved
Bug? Feature? Example? PR it! Or hop into Discord to discuss type utilities and Reliverse tools 💜
git clone https://github.com/reliverse/retyper
cd retyper
bun iMIT © blefnk Nazar Kornienko
Part of the Reliverse ecosystem