-
-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(types): Calculate Context<Env> type for each handler #3675
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3675 +/- ##
=======================================
Coverage 91.31% 91.31%
=======================================
Files 161 161
Lines 10240 10240
Branches 2999 2998 -1
=======================================
Hits 9351 9351
Misses 888 888
Partials 1 1 ☔ View full report in Codecov by Sentry. |
I used the following code to investigate how much type calculation costs vary. import { Hono } from '../../../src'
import { createMiddleware } from '../../../src/helper/factory'
const mw1 = createMiddleware<{ Variables: { foo: string } }>(async (c, next) => {
c.set('foo', 'bar')
await next()
})
const mw2 = createMiddleware<{ Variables: { bar: string } }>(async (c, next) => {
c.set('bar', 'baz')
await next()
})
const mw3 = createMiddleware<{ Variables: { baz: string } }>(async (c, next) => {
c.set('baz', 'qux')
await next()
})
const mw4 = createMiddleware<{ Variables: { qux: string } }>(async (c, next) => {
c.set('qux', 'quux')
await next()
})
const mw5 = createMiddleware<{ Variables: { quux: string } }>(async (c, next) => {
c.set('quux', 'corge')
await next()
})
const mw6 = createMiddleware<{ Variables: { corge: string } }>(async (c, next) => {
c.set('corge', 'grault')
await next()
})
const mw7 = createMiddleware<{ Variables: { grault: string } }>(async (c, next) => {
c.set('grault', 'garply')
await next()
})
const mw8 = createMiddleware<{ Variables: { garply: string } }>(async (c, next) => {
c.set('garply', 'waldo')
await next()
})
export const app = new Hono().get(
'/route1',
mw1,
mw2,
mw3,
mw4,
mw5,
mw6,
mw7,
mw8,
// Context<Env> at this 'c' is combined from all middlewares
async (c, next) => {
await next()
},
(c) => {
const foo = c.get('foo')
return c.json({ foo })
}
) |
The result is as follows.
|
@sushichan044 Thank you for handling it. Exactly. I also thought the decrease in performance would possibly be a pain point.
@m-shaka What do you think of it? |
8f4ca23
to
1af264b
Compare
1af264b
to
173a2cf
Compare
173a2cf
to
5308071
Compare
I haven't merged yet because I'm concerned about performance. TypeScript performance is critical to DX, so I am considering whether or not to allow performance degradation due to this change. |
Yes, the experience gained, and performance degraded by this PR should be discussed in depth. |
I've considered it. In my current opinion, we can merge this. It introduces a performance decrease for type inferences, but it is also important that the type inference works as expected correctly. Fortunately, the tiny changes #3836 and #3837 can improve the TypeScript performance. So, first, merge this PR and improve the types with some techniques (though I don't know if we have room for improvement). Are you okay? |
Hello @yusukebe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@sushichan044 Thanks! I'll merge this now. |
closes #3587
This PR makes all
Context<Env>
in handlers respect type changes made by previous handlers.The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code