Hono RPC Type Inference Issues with Nested Routes in Monorepo
Hi everyone! I'm fairly new to Hono and still learning, so I apologize if I'm missing something obvious or doing something wrong. I'm working on a monorepo project using Next.js (frontend) and Hono (backend) with Hono RPC for type-safe API communication. I'm experiencing issues where some API routes are not properly typed on the frontend, possibly due to deep nesting in the type definitions.
import { Hono } from "hono";import { signageRoutes, publicSignageRoutes } from "./modules/signage";const apiRoutes = new Hono() .route("/signages", signageRoutes) .route("/public/signages", publicSignageRoutes);const app = new Hono() .route("/api", apiRoutes);export type AppType = typeof apiRoutes;
import { Hono } from "hono";import { signageRoutes, publicSignageRoutes } from "./modules/signage";const apiRoutes = new Hono() .route("/signages", signageRoutes) .route("/public/signages", publicSignageRoutes);const app = new Hono() .route("/api", apiRoutes);export type AppType = typeof apiRoutes;
Frontend (apps/web/src/lib/api-client.ts):
import type { AppType } from "@api/index";import { hc } from "hono/client";export const apiClient = hc<AppType>(API_URL, { init: { credentials: "include", },});
import type { AppType } from "@api/index";import { hc } from "hono/client";export const apiClient = hc<AppType>(API_URL, { init: { credentials: "include", },});
The Problem: When trying to use the RPC client on the frontend, some routes work fine but others don't seem to have proper type inference. For example, this might work: apiClient.api.signages.$post() but nested routes or certain endpoints lose type information.
Questions: 1. Is there a recommended pattern for exporting types when using nested routes (route() chaining)? 2. If there's a better approach for type sharing, I'd love to hear about it. I tried Zod OpenAPI Hono but couldn't figure out how to integrate it with Better Auth, so I gave up on that approach. 3. If there are any existing threads discussing and solving similar issues, I'd appreciate if you could share them.
Any insights or suggestions would be greatly appreciated! Thanks in advance!