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

@itsezz/try-catch

v1.3.0

Published

A TypeScript utility for elegant error handling with Result types

Readme

@itsezz/try-catch

npm version

Type-safe error handling for TypeScript. No more try/catch blocks.

Install

npm install @itsezz/try-catch

Result Types

type Result<T, E = Error> = Success<T> | Failure<E>;

type Success<T> = { data: T; error?: never; ok: true };
type Failure<E> = { data?: never; error: E; ok: false };

Use result.ok to check success/failure with full TypeScript narrowing.

Quick Start

import { tryCatch, isError } from '@itsezz/try-catch';

// Sync
const result = tryCatch(() => JSON.parse('{"name":"user"}'));

if (result.ok) console.log(result.data.name); // "user"
else console.error(result.error);

// Async
const user = await tryCatch(fetch('/api/user').then(r => r.json()));

if (isError(user)) return null;
return user.data;

API

Functions

| Function | Short alias | Returns | Description | | ----------------- | ----------- | --------------------------- | ----------------------- | | tryCatch() | t() | Result \| Promise<Result> | Auto-detects sync/async | | tryCatchSync() | tc() | Result | Guaranteed sync | | tryCatchAsync() | tca() | Promise<Result> | Guaranteed async |

Note: tryCatch may not correctly infer if the result is Promise<Result<T,E>> or Result<T,E> in certain conditions and defaults to Promise<Result<T,E>> when unsure. Use explicit variants for guaranteed type safety.

Create Results

success(42)     // { data: 42, ok: true }
failure('err')  // { error: 'err', ok: false }

Check Results

if (result.ok) result.data; // Success<T>
else result.error;          // Failure<E>

isSuccess(result);  // type guard
isError(result);    // type guard

Transform Results

map(result, fn)             // transform success value
flatMap(result, fn)         // chain operations returning Result
unwrapOr(result, default)   // get value or default
unwrapOrElse(result, fn)    // get value or compute from error
match(result, {             // pattern matching
  success: (data) => ...,
  failure: (error) => ...
})

Examples

Safe JSON Parsing

const json = tryCatch(() => JSON.parse(input));

if (!json.ok) return { error: 'Invalid JSON' };
return json.data;

API Request with Type Safety

interface User {
  id: number;
  name: string;
}

const fetchUser = async (): Promise<User | null> => {
  const result = await tryCatch(fetch('/api/user').then(r => r.json()));
  if (result.ok) return result.data;
  return null;
};

Validation

const validate = (input: unknown): Result<User, string[]> => {
  const parsed = tryCatch(() => JSON.parse(input as string));
  if (!parsed.ok) return failure(['Invalid JSON']);

  const errors: string[] = [];
  if (!parsed.data.name) errors.push('Name required');

  return errors.length > 0 ? failure(errors) : success(parsed.data);
};

Functional Pipeline

const result = unwrapOr(
  flatMap(success('42'), s => success(parseInt(s))),
  0
); // 84

FAQ

Default error type? Error. Use generics for custom types:

tryCatch<number, ApiError>(() => fetchData())

When to use each variant?

  • tryCatch: Don't care about sync/async distinction
  • tryCatchSync: Need guaranteed sync return
  • tryCatchAsync: Need guaranteed Promise<Result> return

License

MIT