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