hono
hono copied to clipboard
Hono infers the output for all client endpoints as an empty object when building type defs
What version of Hono are you using?
4.0.6
What runtime/platform is your app running on?
Cloudflare Pages, Vite
What steps can reproduce the bug?
-
Create a simple REST api with Hono and Vite, including zod validators.
-
Enable library mode in the Vite config file, and add the
vite-plugin-dts
plugin. -
Create a client module, which exports a Hono client, typed using the app object:
export const client = hc<typeof app>("http://localhost:5173/");
-
Build the package.
-
Open the generated
.d.ts
file for the client, and see that all theoutput
fields are inferred as{}
.
Source code for the project I'm experiencing this in is available here: https://github.com/lipu-linku/sona/tree/main/api. Build the project using pnpm run build --mode lib
and open api/dist/client/client.d.ts
to see the result.
What is the expected behavior?
The output types should be correctly inferred from the types used in the actual project.
What do you see instead?
All the output types are {}
, even though in dev (hovering over the output types in-editor) they're correctly inferred.
Additional information
No response
Hi, saw this was unmarked as a bug, if there's a solution or if this is intended behavior, I'd love to know
I think this is a TypeScript matter, not Hono's matter.
Upon investigation, the reason is that the following ToSchema is not correctly expanded.
That should be as follows:
{
"/": {
$get: {
input: {};
output: {
foo: boolean;
};
};
};
}
This is because Hono's type definitions are complex, but that is to create RPC mode. Right now we are unable to respond to address your issue. If you have a solution, please let me know.
I've been experiencing a similar thing with the client, in my case I have a monorepo where I generate the client on an api
workspace and install and import it on a web
workspace.
I had that within the api
workspace the types worked perfect, however whenever I imported it within the web
workspace the response types were all ClientResponse<{}>
.
I did some digging and it was indeed a TS "problem", as it only happened when using baseUrl
and paths
on the api
side.
When I remove them, it works like a charm.
@TheOnlyTails I see that in your repo you are also using TS path
config, could you try a version without it, just to check it is indeed the culprit?
I don't know how this was fixed, but in the latest release it all works great!
I too have a monorepo, and I had the same issue as @jvcmanke.
In my case, I had to set strict
to true
in both my tsconfig.json files, and it was fixed.
{
"compilerOptions": {
"noImplicitAny": false,
"target": "esnext",
"module": "esnext",
"lib": ["ES2019", "DOM", "DOM.Iterable"],
"moduleResolution": "node",
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"jsxImportSource": "lestin",
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
+ "strict": true,
},
}
If setting "strict": true
in tsconfig.json files is always needed for this, it might be good to mention it in https://hono.dev/guides/rpc.
@yusukebe
Hi @movahhedi
That's a good idea. Can you work on it?
On it.
@movahhedi Thanks!
I have it strict and does not work on react vite, it is strict both on server and react vite
@leognmotta, did you restart VSCode? Or run the command "TypeScript: Restart TS Server"? And do you have hono installed at the root of the monorepo?
@leognmotta, did you restart VSCode? Or run the command "TypeScript: Restart TS Server"? And do you have hono installed at the root of the monorepo?
I found it was about ts paths config
@leognmotta, did you restart VSCode? Or run the command "TypeScript: Restart TS Server"? And do you have hono installed at the root of the monorepo?
I found it was about ts paths config
can you please give a little details of the solution?
I'm currently facing the same issue on my monorepo project. Hono is installed in the root and my frontend is vite + vue3
@leognmotta, did you restart VSCode? Or run the command "TypeScript: Restart TS Server"? And do you have hono installed at the root of the monorepo?
I found it was about ts paths config
can you please give a little details of the solution?
I'm currently facing the same issue on my monorepo project.
Hono is installed in the root and my frontend is vite + vue3
I basically stopped using paths setting in ts config, so instead importing using alias @/ I just import directly "../../../"