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