Getting Started with Typed Fetch
Typed Fetch is a type-safe HTTP client for TypeScript that never throws exceptions. Inspired by Go's error handling pattern, it's built on top of the native Fetch API.
What the Project Exports
From the main entry (@pbpeterson/typed-fetch):
typedFetch— The main function. Same signature asfetch(), returns{ response, error }instead of throwing.isHttpError— Type guard to check if a value is an HTTP error.isNetworkError— Type guard to check if a value is a network error.statusCodeErrorMap— Map of status codes to error class constructors.httpErrors— Array of all 40 error class constructors.- Error classes — 40 individual HTTP error classes +
NetworkError+BaseHttpError. - Types —
ClientErrors,ServerErrors,HttpErrors,TypedFetchError,TypedHeaders,StrictHeaders,HttpMethods.
From the errors subpath (@pbpeterson/typed-fetch/errors):
- All error classes, individually tree-shakeable.
Why This Approach
- Never Throws — Errors are returned as values, making handling explicit and predictable.
- Type-Safe — Literal status types (
404, notnumber), generic error bodies, discriminated unions. - Built on Native Fetch — Same API, same options, same types. Zero learning curve.
- Tree-Shakeable — Import from
./errorsto pull only the classes you need. - Type Guards —
isHttpError()andisNetworkError()work reliably across package boundaries.
For installation and basic usage, see the next sections.