npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rodrigosdev/okerr

v0.0.2

Published

A lightweight type-safe Result Library for TypeScript with explicit Ok/Err handling

Downloads

12

Readme

Okerr

A small, type-safe Result type for TypeScript: every outcome is either success (ok) or failure (err), so callers handle both paths explicitly instead of relying only on exceptions.

Highly inspired by better-result and Effect.

Install


The Result shape

A Result<T, E> is a discriminated union:

  • Success: { ok: true, value: T }
  • Failure: { ok: false, error: E }

Branch on result.ok (or use isOk / isErr / match) so TypeScript narrows value or error correctly.

Quick start

import { err, fn, isOk, match, ok, orElse, value } from 'okerr';

function parsePositive(s: string): Result<number, string> {
	const n = Number(s);
	if (!Number.isFinite(n) || n <= 0) {
		return err('expected a positive number');
	}
	return ok(n);
}

const r = parsePositive('42');

if (isOk(r)) {
	console.log(r.value); // number
} else {
	console.error(r.error); // string
}

// Or collapse to one expression:
const doubled = match(r, {
	ok: (n) => n * 2,
	err: () => 0,
});

API

ok(value) / err(error)

Construct a success or error result. Payloads may be any type, including undefined or null.

ok(1); // Result<number, never>
err('nope'); // Result<never, string>

isOk(result) / isErr(result)

Type guards for narrowing. Prefer these or match when you want exhaustive handling.

value(result)

Returns the success value, or throws the stored error if the result is err. Use when failure should propagate as an exception.

orElse(resultOrPromise, defaultValue)

Returns the success value, or defaultValue if the result is an error. Accepts either a Result or a Promise<Result> and always returns a Promise of the success type.

await orElse(ok(1), 0); // 1
await orElse(err('x'), 0); // 0
await orElse(Promise.resolve(ok(2)), 0); // 2

match(result, { ok, err })

Runs exactly one arm and returns a single type R. Both callbacks are required, which keeps handling complete and helps inference.

const out = match(ok(42), {
	ok: (n) => n * 2,
	err: () => 0,
}); // 84

fn(inner)

Wraps a synchronous or async function so it never throws: successes become ok(...), and failures become err(Error). Non-Error throws are normalized to Error (message from JSON.stringify when possible).

The returned function is async and has the type ResultFn<Args, T, Error> (see ResultFn in the package typings).

const divide = fn((a: number, b: number) => {
	if (b === 0) throw new Error('division by zero');
	return a / b;
});

const r = await divide(10, 2);
// { ok: true, value: 5 }

const bad = await divide(10, 0);
// { ok: false, error: Error }

const message = match(bad, {
	ok: () => 'ok',
	err: (e) => e.message,
});

Types

  • Result<T, E> — success or failure union.
  • ResultFn<A, T, E> — function from arguments A to Promise<Result<T, E>>.

Patterns

Composable pipeline with fn + match:

const parse = fn((s: string) => {
	const n = Number(s);
	if (Number.isNaN(n)) throw new Error('not a number');
	return n;
});

const first = await parse('12');
const doubled = match(first, {
	ok: (n) => n * 2,
	err: () => 0,
});

Interop with code that throws: wrap boundaries with fn, keep the rest of your code on Result + match / guards.

License

MIT