Skip to main content

Overview

The Hono client provides several utility types for extracting request and response types from your API endpoints, enabling full type safety in your client code.

Import

Type Inference Utilities

InferRequestType

Extracts the request type (arguments) from a client endpoint method. Type Signature:
Usage:
Example with validation:

InferResponseType

Extracts the response type from a client endpoint method, with optional status code filtering. Type Signature:
endpoint method
required
The client endpoint method to infer from
number
default:"any status code"
Optional status code to filter by. Only returns the response type for matching status codes.
Usage:
Example with multiple response types:

Client Configuration Types

ClientRequestOptions

Configuration options for the RPC client, used both at client creation and per-request. Type Definition:
typeof fetch
Custom fetch implementation. Useful for testing with MSW or using node-fetch.
Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>)
Headers to include with requests. Can be static object or async function for dynamic headers.
RequestInit
Standard RequestInit options. Takes highest priority and can override Hono’s automatic settings.
BuildSearchParamsFn
Custom function to serialize query parameters.
(...args: ConstructorParameters<typeof WebSocket>) => WebSocket
Custom WebSocket constructor.
Usage:

BuildSearchParamsFn

Function type for custom query parameter serialization. Type Definition:
Usage:

Response Types

ClientResponse

Type-safe response interface that extends the standard Response with inferred types. Type Definition:
generic
The response body type (inferred from server route)
number
The HTTP status code
ResponseFormat
The response format (‘json’, ‘text’, etc.)
Key Features:
  • The ok property type is narrowed based on status code
  • The json() method returns Promise<T> with the inferred response type
  • The text() method returns Promise<string> (or Promise<T> for text responses)
Usage:

FilterClientResponseByStatusCode

Filters a ClientResponse type to only include specific status codes. Type Definition:
Usage:

Fetch Type

A helper type for creating functions that wrap client endpoints. Type Definition:
Usage:

Advanced Types

ApplyGlobalResponse

Applies global response definitions to your Hono app type. Type Definition:
Usage:

Complete Example

Here’s a complete example using all the main client types: