H
Hono•2mo ago
Charlie Lamb

Hono RPC TypeScript error

Hey I'm getting a really frusttrating error where TypeScript is not inferring types in a monorepo. I'm building a monorepo using Nextjs for the frontend and bun & hono for the backend. I'm trying to use hono rpc and I'm exporting the type of my router like this in my app.ts file in my backend project: import createApp from '@/lib/create-app' import configureOpenAPI from '@/lib/configure-open-api' import index from '@/routes/index.route' import users from '@/routes/user/user.index' import connects from '@/routes/connects/connects.index' const app = createApp() const routes = [index, users, connects] as const configureOpenAPI(app) routes.forEach((route) => { app.route('/', route) }) export default app export type AppType = (typeof routes)[number] and then trying to import it in my frontend project in my client.ts file import type { AppType } from '../backend/src/app' import { hc } from 'hono/client' const client = hc<AppType>('http://localhost:8000') export default client This isn't working and my Type is being inferred as any. When I hover over app type in app.ts in my backend - I see this type type AppType = OpenAPIHono<AppBindings, { "/": { $get: { input: {}; output: { message: string; }; outputFormat: "json" | "text"; status: 200; }; }; }, "/"> | OpenAPIHono<AppBindings, { ...; }, "/"> | OpenAPIHono<...> It 100% has the right type. When I hover over AppType in client.ts however I see this: (alias) type AppType = any import AppType Anyone know how I can fix this issue? Repo to see for yourself + all tsconfigs: https://github.com/charlietlamb/postpad Thanks for all help 🙂
GitHub
GitHub - charlietlamb/postpad
Contribute to charlietlamb/postpad development by creating an account on GitHub.
4 Replies
Tempo
Tempo•2mo ago
Hi, I stumbled exactly on the same issue yesterday. I have tried with the compiled method (https://hono.dev/docs/guides/rpc#compile-your-code-before-using-it-recommended) but the issue remain Could you solve your issue ?
Charlie Lamb
Charlie LambOP•2mo ago
That looks like it could work thanks - I’ll let you know when I’m back home, appreciate the help
Tempo
Tempo•2mo ago
Ok, just some follow up, I could fix the issue on my side: 1: add "composite": true in your backend tsconfig compilerOptions 2: add reference to the backend config in your frontend tsconfig
"references": [
{ "path": "../backend/tsconfig.json" }
],
"references": [
{ "path": "../backend/tsconfig.json" }
],
now my type inference are working properly. Thanks to this github issue : https://github.com/honojs/hono/issues/3485
Charlie Lamb
Charlie LambOP•2mo ago
This fixed my issue - thank you so much, a lot of time saved 🙂
Want results from more Discord servers?
Add your server