hono icon indicating copy to clipboard operation
hono copied to clipboard

Absolute imports break `typeof app` for rpc client.

Open nvsd opened this issue 1 year ago • 6 comments

What version of Hono are you using?

3.10.2

What runtime/platform is your app running on?

bun

What steps can reproduce the bug?

I have a separate file that has a single test route in it

// test-route.ts
import { Hono } from 'hono';

export const route = new Hono().get('/', async (c) => c.text('Hello World!'));

in the below code notice import { route } from 'test-route'; is an absolute import which breaks the type (see screenshot)

// index.ts
import { Hono } from 'hono';
import { route } from 'test-route';

const app = new Hono().route('/test', route);

export type AppType = typeof app;

export default app;

in the below code, notice that import { route } from './test-route'; is a relative import which works (see screenshot)

import { Hono } from 'hono';
import { route } from './test-route';

const app = new Hono().route('/test', route);

export type AppType = typeof app;

export default app;

What is the expected behavior?

I get strong types with relative imports. I would expect the same behavior with absolute imports. image

What do you see instead?

I have a broken type with absolute imports. image

Additional information

No response

nvsd avatar Nov 29 '23 04:11 nvsd

I have found that this also happens when using path aliases. Version 4.0.10

Holmes-EH avatar Mar 17 '24 11:03 Holmes-EH

this also happen when I using mono repos, in this case, the api was move to api-package and the client side - react app import the api-package

auvansang avatar Mar 30 '24 00:03 auvansang

I am not ignoring the issue because I don't see a good solution. If anyone knows of a better way, please let me know.

yusukebe avatar Mar 30 '24 09:03 yusukebe

I think add the test-route module from node_modules to paths mapping can help this to work @yusukebe

This is not something that must be handled by Hono.

// Path mapping
"paths": {
  "@lib/*": ["lib/*"],
  "test-route": ["node_modules/test-route-lib"] // Maps all imports starting with "~/" to "./lib/"
},

Also check: https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths#typescript-transform-paths-plugin, basically the idea is to transform absolute imports to relative imports.

Screenshot 2024-05-06 224325

fzn0x avatar May 06 '24 15:05 fzn0x