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 theEnv type.
Bindings
Bindings are environment-specific values (like environment variables on Cloudflare Workers):Variables
Variables are values you set and get usingc.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
Use type instead of interface for Env
Use type instead of interface for Env
Always use
type to define your environment generics, not interface.Define types at the app level
Define types at the app level
Define your environment types once when creating the Hono instance.
Leverage type inference
Leverage type inference
Let TypeScript infer types where possible instead of explicit annotations.
Use validation libraries with good types
Use validation libraries with good types
Use Zod, Valibot, or similar libraries that provide excellent type inference.
Export your app type for RPC
Export your app type for RPC
Export your app type to enable type-safe RPC clients.
Related
- Validation - Type-safe validation patterns
- Testing - Test with full type safety
- Error Handling - Type-safe error handling