惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
P
Proofpoint News Feed
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
博客园_首页
I
InfoQ
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
美团技术团队
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Rest In Peace, Throw: TypeScript error handling 22x faste...
Cesar Marcan · 2026-05-12 · via DEV Community

Cesar Marcano

Example code using ripthrow

In TypeScript, we’ve been told that try/catch is the standard for error handling. But there’s a catch: native exceptions are essentially invisible GOTO statements that jump over layers of your application, making it incredibly hard to reason about state.

Even worse, because JavaScript allows you to throw anything (from strings to dates), TypeScript is forced to treat every caught error as unknown. This creates a type-erasure effect where your function's failure states become invisible to the compiler and uncontracted in your signatures.

I built ripthrow to fix this. It’s a 1.6KB, zero-dependency library that brings Rust-inspired error handling to TypeScript with a focus on pragmatism and raw performance.

22x Performance over native throw

Most people don't realize how expensive throw actually is. Based on my benchmarks (running on Bun 1.3), the difference is staggering:

Pattern Operations/sec Latency vs Native
Err() (ripthrow) 25,357,831 41.6 ns
throw (Native) 1,182,517 1047.0 ns 22x slower

By returning a Result instead of throwing, you get the same speed as a manual object literal because ripthrow uses POJOs (Plain Old JavaScript Objects) instead of expensive class allocations. In real-world scenarios, the overhead is a negligible 15-20 ns per operation.

How it looks in practice

Stop nesting if statements and losing your types. With the AsyncResultBuilder, you can create fluent pipelines that are 100% type-safe:

const Errors = createErrors({
  UserNotFound: { message: (id: number) => `User #${id} not found` },
});

function getUserName(userId: number) {
  // Wrap promises safely without try/catch blocks
  // Note: ripthrow offers multiple wrapping strategies; see the GitHub Wiki for details.
  return AsyncResultBuilder.safeAsync(db.users.findById(userId))
    .mapErr(() => Errors.UserNotFound(userId))
    .map((user) => user.name)
}

const userName = await getUserName(123);

if (userName.ok) {
  console.log(`Hi! ${userName.value}`); // Fully typed success
} else {
  console.log(userName.error.message); // "User #123 not found"
}

Enter fullscreen mode Exit fullscreen mode

Exhaustive Matching

One of the biggest risks of manual error handling is forgetting a case. While other libraries require manual switch statements or never checks, ripthrow has a built-in fluent API for this.

If you add a new error type to your application and forget to handle it, your code won't compile.

// This will throw a type error at compile-time if 'NetworkError' is not handled
// Note: defining explicit `data: User` type is required to ensure that you're handling all the errors
const data: User = matchErr(result)
  .on(Errors.UserNotFound, (err) => ...)
  .exhaustive(); 

Enter fullscreen mode Exit fullscreen mode

Why ripthrow?

While other libraries exist, ripthrow is designed as a "missing operator" for modern ESM:

  • Minimalist: Only ~1.6 KB (min+gzip).
  • No Classes: Uses simple { ok: true, value } structures for maximum speed.
  • Exhaustive Matching: Use .exhaustive() to ensure at compile-time that you’ve handled every possible error variant.
  • Collision-Free: Unique Symbols identify error types, preventing collisions between different packages.

If you want to make your TypeScript applications more resilient and faster, ripthrow is now at v3.0 - Stable.

Check it out on GitHub: MechanicalLabs/ripthrow