Skip to main content

Overview

Hono is built with TypeScript and provides excellent type safety out of the box. With Hono’s type system, you get full autocomplete and type checking for routes, middleware, validation, and more.

Environment Types

Define types for your application’s environment variables and context variables using the Env type.

Bindings

Bindings are environment-specific values (like environment variables on Cloudflare Workers):

Variables

Variables are values you set and get using c.set() and c.get():

Combined Environment

Type-Safe Routing

Hono infers types from your route patterns:

Validation Types

Get full type safety with validation:

Using Zod for Validation

Zod provides even better type inference:

Generic Types

Understand Hono’s key generic types:

Env

The environment type containing Bindings and Variables:

Handler

The handler function type:

MiddlewareHandler

The middleware function type:

Custom Middleware Types

Create type-safe custom middleware:

Response Types

Type your JSON responses:

Context Types

Import and use Context types in your code:

Type Utilities

Hono provides type utilities for common patterns:

Module Augmentation

Extend Hono’s types using module augmentation:

RPC Type Safety

Get end-to-end type safety with Hono’s RPC client:

Best Practices

Always use type to define your environment generics, not interface.
Define your environment types once when creating the Hono instance.
Let TypeScript infer types where possible instead of explicit annotations.
Use Zod, Valibot, or similar libraries that provide excellent type inference.
Export your app type to enable type-safe RPC clients.