Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

I missed Go's `if err != nil`, so I built errval for TypeScript

说明与用途

errval 返回 [err, value] 元组,提供显式错误指示、类型安全和完整的情况处理,提升 TypeScript 代码的可靠性。

问题

TypeScript developers lack explicit error handling like Go’s if err != nil, leading to ambiguous catch (e) blocks and runtime errors.

结果

errval provides explicit error tuples, inferred error unions, and exhaustive matching, improving type safety and reducing runtime errors. Benchmarks show comparable performance to neverthrow and far better than try/catch on failure paths.

工作流

  • 1. Call an async function that returns [err, value] via await getUser(id). 2. Check if (err) and handle or return early. 3. Use the value safely without ! or type assertions. 4. Handle errors with match(err, {NotFound:..., Forbidden:..., DbError:...}) ensuring all cases compile.
  • 5. Add new error types in services; the handler’s match will fail to compile until a case is added.

证据

I benchmarked it. A request handler where half the requests fail, Node 24.16, per request: neverthrow: 198 ns, errval: 226 ns, try/catch: 2,623 ns

来源

Return a new error type from the service and the match in the handler will stop compiling until it has a case for it.

来源

  • 记录日期: 2026-09-22T02:45:38.986Z