Skip to main content
ofetch provides excellent TypeScript support with generic types for type-safe API calls.

Response Type Inference

Use TypeScript generics to specify the expected response type:

Nested Response Types

For APIs that wrap data in a response envelope:

Array Responses

Response Type with responseType Option

The second generic parameter allows you to specify the response format:

Type-Safe API Client

Create a fully typed API client:

Hooks with Type Safety

Hooks are also fully typed:

Error Handling with Types

Type Definitions

From src/types.ts:5-16:
The MappedResponseType helper (from src/types.ts:132-135):
This means:
  • responseType: 'blob' → returns Blob
  • responseType: 'text' → returns string
  • responseType: 'arrayBuffer' → returns ArrayBuffer
  • responseType: 'stream' → returns ReadableStream<Uint8Array>
  • responseType: 'json' (default) → returns your generic type T