rpc-typesafelisted
Install: claude install-skill smicolon/ai-kit
# Hono RPC Type Safety
Patterns for type-safe client-server communication with Hono.
## Server Setup
### Export App Type
```typescript
// src/index.ts
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { z } from 'zod'
const app = new Hono()
// Define routes with validators for full type inference
const routes = app
.get('/users', async (c) => {
return c.json({ users: [{ id: '1', name: 'John' }] })
})
.post('/users',
zValidator('json', z.object({
email: z.string().email(),
name: z.string()
})),
async (c) => {
const data = c.req.valid('json')
return c.json({ id: crypto.randomUUID(), ...data }, 201)
}
)
.get('/users/:id', async (c) => {
const id = c.req.param('id')
return c.json({ id, name: 'John' })
})
export default routes
// CRITICAL: Export type for client
export type AppType = typeof routes
```
### Route Chaining for Type Inference
```typescript
// Chain routes to preserve types
const userRoutes = new Hono()
.get('/', async (c) => c.json({ users: [] }))
.post('/',
zValidator('json', createUserSchema),
async (c) => c.json({ id: '1' }, 201)
)
const postRoutes = new Hono()
.get('/', async (c) => c.json({ posts: [] }))
.post('/',
zValidator('json', createPostSchema),
async (c) => c.json({ id: '1' }, 201)
)
// Compose and export
const app = new Hono()
.route('/users', userRoutes)
.route('/posts', postRoutes)
export type AppType = typeof a