Skip to content

Types

TypedResponse<T>

Extends Response with a typed json() method:

typescript
interface TypedResponse<T> extends Response {
  json(): Promise<T>;
}

ClientErrors

Union of all 29 client error (4xx) instances:

typescript
type ClientErrors =
  | BadRequestError
  | UnauthorizedError
  | NotFoundError
  // ... all 29 client error classes

ServerErrors

Union of all 11 server error (5xx) instances:

typescript
type ServerErrors =
  | InternalServerError
  | BadGatewayError
  | ServiceUnavailableError
  // ... all 11 server error classes

HttpErrors

Constructor type for any HTTP error class:

typescript
type HttpErrors = (typeof httpErrors)[number];

TypedFetchError

Union of all possible error types returned by typedFetch:

typescript
type TypedFetchError = HttpErrors | NetworkError;

HttpMethods

Union of all standard HTTP method strings:

typescript
type HttpMethods =
  | "GET" | "POST" | "PUT" | "PATCH" | "DELETE"
  | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE";

StrictHeaders

HTTP headers type with IntelliSense for common header names and typed values:

typescript
type StrictHeaders = {
  "Content-Type"?: "application/json" | "text/plain" | ...;
  Authorization?: `${string} ${string}`;
  Accept?: "application/json" | "*/*" | ...;
  "Cache-Control"?: "no-cache" | "no-store" | ...;
  // ... 30+ common headers with typed values
  [key: string]: string | undefined; // custom headers allowed
};

TypedHeaders

Accepts both StrictHeaders (with IntelliSense) and raw HeadersInit:

typescript
type TypedHeaders = StrictHeaders | HeadersInit;

Discriminated Union Return Type

typedFetch returns a discriminated union — checking error narrows both fields:

typescript
const { response, error } = await typedFetch<User>('/api/users');

if (error === null) {
  // TypeScript knows: response is TypedResponse<User>
  const user = await response.json(); // Type: User
} else {
  // TypeScript knows: response is null
  // error is ClientErrors | ServerErrors | NetworkError
}

With specific client errors:

typescript
const { response, error } = await typedFetch<User, NotFoundError>('/api/users/1');

if (error !== null) {
  // error is NotFoundError | ServerErrors | NetworkError
}

Released under the MIT License.