try-error Documentation

Getting Started
Introduction

Introduction to try-error

Lightweight, progressive, type-safe error handling for TypeScript that bridges the gap between traditional try/catch and heavy functional programming approaches.

What is try-error?

try-error provides errors as values with zero-overhead success paths,progressive adoption, and a developer-first experience. It allows you to handle errors explicitly without the complexity of monads or the verbosity of traditional try/catch blocks.

Zero Overhead
Success values are returned directly. Error objects only created when needed.
Progressive
Start simple, add complexity as needed. No need to rewrite existing code.
Type Safe
Full TypeScript support with discriminated unions and type inference.

The Problem

Current TypeScript error handling solutions have significant gaps:

Traditional try/catch: Verbose, error-prone, poor composability, and makes it easy to forget error handling.
Heavy FP libraries: Foreign feel for JavaScript developers, runtime overhead, steep learning curve.
Existing Result libraries: Missing async support, poor developer experience, limited real-world patterns.

The Solution

try-error provides a middle ground that feels natural to JavaScript/TypeScript developers while providing the benefits of explicit error handling.

Before: Traditional try/catch
Traditional Error Handlingtypescript
try {
  const response = await fetch("/api/user");
  const user = await response.json();
  return user;
} catch (error) {
  console.error("Something failed:", error);
  return null;
}
Issues: Easy to forget, unclear error types, poor composability
After: try-error
try-error Approachtypescript
const result = await tryAsync(async () => {
  const response = await fetch("/api/user");
  return response.json();
});

if (isTryError(result)) {
  console.error("API failed:", result.message);
  return null;
}

return result; // Type-safe success value
Benefits: Explicit, type-safe, composable, rich error context

Key Benefits

Errors as Values: No hidden control flow, explicit error handling
Zero Overhead: Success path has no runtime cost
Rich Error Context: Source location, timestamps, custom context
Composable: Chain operations, combine results, transform errors
TypeScript Native: Full type inference and safety
Progressive: Adopt incrementally, no big rewrites

Next Steps