ContextVariableMap not recognized during build from other package in monorepo
I created
src/hodo.d.ts
file in my Hono project:
Then, the type was inferred out of the box:
And, I configured the RPC feature in my React project:
It worked like a charm. but then I tried build command and encountered an error:
Does ContextVariableMap
type declaration not work with RPC feature?25 Replies
I think I found a solution.
src/hono.d.ts
:
src/index.ts
:
Did you add
export {}
to your hono.d.ts file?
Otherwise it won’t be globalI didn't and it works well?! don't know what the difference is
That did work or it didn’t?
No difference whether adding it or not. both works.
This is with the original code in there not the Variable type correct?
I don't understand your question. I'm totally new to Hono
I’m asking if you testing it with the original code you provided. Not the updated one
I did with the updated one. I'll try your suggestion with the original code
Yes
The same error happens from the React project.
I have to use screenshots on not on my computer right now. But add this is no different from using ContextVariableMap
Which means this is doing nothing
If I remove the ContextVariableMap declaration, and remain
const app = new Hono<{ Variables: Variables }>();
, typescript can't recognize it.
ofc I can do this as well:
but this way, I have to add the type to all my route filesI’m not sure what’s going on here if you take at look at my example project you’ll see how I’ve declared it and it has no issues. I don’t have to use type generics on each instance of Hono https://github.com/NicoPlyley/hono-auth/blob/main/src/app.d.ts
GitHub
hono-auth/src/app.d.ts at main · NicoPlyley/hono-auth
An example app with authentication using Hono, Drizzle, and D1. Running on CF Workers - NicoPlyley/hono-auth
I guess, the difference is that I want to use the type from another package with RPC feature. I configured a monorepo:
package.json
in the root directory:
The error happens when I try build command bun run build
from the web
project.But you said it goes away when you use type generics/your updated code right?
Yes
Yeah that’s weird because you should be able to use just ContextVariableMap for everything
I might have to play around with it with RPC and see
I think I can create a reproducible project
I think I see the issue here the second. On your entry point file to Hono. I bet if you set the context variable map there and not in a separate file it would work. Because of how modules work in typescripts the frontend doesn’t know that file exists
You're right. Moving the type declaration part into
index.ts
file works too!
Or, I can just import the type file into the entry file:
import './hono.d.ts';
This looks cleaner
not sure why export {}
doesn't resolve my problem though. Thank you for your helpBecause the tsconfig is react is only going to look for ts modules insides of the react project files not the ones for Hono
Now that that’s it’s linked to the code base it know where to find them
I see. Maybe, it would be great to add this to the documentation in the Context or RPC page
Yes I will check tomorrow so see where it can fit in
Because I believe the RPC is one of the killer features of Hono. Everyone, including me, wants E2E type safety to reduce unnecessary duplicated code. Again, really appreciated