How to correctly type and access optional middleware variables in Hono context? #4274
Unanswered
DanielBoxer
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When you have middleware that sets a variable in the context that's only used in some routes, how are you supposed to tell TypeScript that the variable is available in the routes using that middleware?
The best approach I've found (in discussion #3257) is to mark the variable as optional in the context type:
Then I have a helper function to access it which imports the AppContext type:
This works, but I was wondering if there's a better way to do this in Hono?
Beta Was this translation helpful? Give feedback.
All reactions