feat(hono-base): introduce ~env type-only attribute in Hono class
#4428
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
honojs/middleware#637
Can not call
.use()fromOpenAPIHono(zod-openapi) without changing the return type into a generalHonoinstanceCurrent solution
People tried adding custom Env to the OpenAPIHono instance
Others also used approach like extending the interface
ContextVariableMapand use type augmentation (like inhono/request-idmiddleware). But both methods have the same issue:.use(loggerMiddleware())in index.ts will risk it being broken on runtime.Solution
By introducing new attribute
~envin Hono class, we can get the inferredEnvtypes from Hono instance that has been added using.use(middleware)The naming
~envand~schema(prefixed with tilde) was choosen to de-prioritize it in autocompletion. By contrast, an underscore-prefixed property would show up before properties/methods with alphanumeric names. As this is mostly just a type-only thing, I also made it return undefined.I'm not sure what kind of tests to add here, as it only forwards the generics
EandSin Hono class, and contains no runtime code.To complement the expose ofSeems that exposing schema would make type inference too deep.~env, I also added~schema, so people can get both generics.