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]viaawait getUser(id). 2. Checkif (err)and handle or return early. 3. Use the value safely without!or type assertions. 4. Handle errors withmatch(err, {NotFound:..., Forbidden:..., DbError:...})ensuring all cases compile. - 5. Add new error types in services; the handler’s
matchwill 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