Hacker News new | ask | show | jobs
Show HN: Ts-result – Rust's Result<T, E> type for explicit error handling in TS (github.com)
2 points by iLikeFunctional 448 days ago
Hey HN,

Like many, I've often found standard error handling in JS/TS (heavy try-catch, ambiguous null returns) a bit cumbersome for representing expected failures explicitly. Inspired by Rust's Result<T, E> enum (Ok(T) | Err(E)), we built ts-result.

It's a lightweight, focused TypeScript library bringing that pattern over:

    Explicit Result<T, E> return types for functions.

    Type safety via discriminated unions and type guards (isOk/isErr).

    Functional composition methods (map, andThen, orElse, etc.) to avoid deep if nesting for success/failure checks.

    Modern TS practices (strict types, ESM/CJS exports).
We were evolving an internal version that started looking like Result anyway, so we decided to build a clean, open-source implementation based on the Rust API.

While there are other libraries (some older, some part of larger FP ecosystems like fp-ts/effect-ts), our goal was a minimal, easy-to-adopt implementation of just the Result pattern itself.

It doesn't magically replicate Rust's ? operator or compiler guarantees, and you still need try-catch at boundaries with exception-throwing code, but we've found it helpful for adding clarity and type-safety to our core logic where specific failure states are expected domain outcomes.

Links:

    GitHub (Source/Docs): https://github.com/TrylonAI/ts-result

    npm (@trylonai/ts-result): https://www.npmjs.com/package/@trylonai/ts-result
We wrote a blog post detailing the motivation, our journey from internal tools, design choices, and how it compares to exceptions or larger FP libraries: https://trylonai.github.io/ts-result/ts-result-blog-post.htm...

Would love to hear your thoughts and feedback

1 comments

Awesome! This should be part of the language - but not sure if anyone is looking into it.