hono icon indicating copy to clipboard operation
hono copied to clipboard

Hono infers the output for all client endpoints as an empty object when building type defs

Open TheOnlyTails opened this issue 1 year ago • 3 comments

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 the output 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

TheOnlyTails avatar Feb 25 '24 10:02 TheOnlyTails

Hi, saw this was unmarked as a bug, if there's a solution or if this is intended behavior, I'd love to know

TheOnlyTails avatar Feb 27 '24 11:02 TheOnlyTails

I think this is a TypeScript matter, not Hono's matter.

Upon investigation, the reason is that the following ToSchema is not correctly expanded.

Screenshot 2024-02-28 at 1 59 05

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.

yusukebe avatar Feb 27 '24 17:02 yusukebe

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?

jvcmanke avatar Apr 24 '24 18:04 jvcmanke

I don't know how this was fixed, but in the latest release it all works great!

TheOnlyTails avatar May 03 '24 13:05 TheOnlyTails

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,
	},
}

movahhedi avatar Jun 02 '24 08:06 movahhedi

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

movahhedi avatar Jun 02 '24 08:06 movahhedi

Hi @movahhedi

That's a good idea. Can you work on it?

yusukebe avatar Jun 02 '24 08:06 yusukebe

On it.

movahhedi avatar Jun 02 '24 08:06 movahhedi

@movahhedi Thanks!

yusukebe avatar Jun 02 '24 08:06 yusukebe

I have it strict and does not work on react vite, it is strict both on server and react vite

leognmotta avatar Jul 07 '24 23:07 leognmotta

@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?

movahhedi avatar Jul 08 '24 01:07 movahhedi

@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 avatar Jul 10 '24 10:07 leognmotta

@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

tutonjason avatar Jul 27 '24 10:07 tutonjason

@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 "../../../"

leognmotta avatar Jul 27 '24 13:07 leognmotta