Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sushichan044
Copy link
Contributor

@sushichan044 sushichan044 commented Nov 15, 2024

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

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.70%. Comparing base (a15bec3) to head (5308071).
Report is 12 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3675   +/-   ##
=======================================
  Coverage   91.70%   91.70%           
=======================================
  Files         159      159           
  Lines       10145    10158   +13     
  Branches     2862     2855    -7     
=======================================
+ Hits         9303     9315   +12     
- Misses        840      842    +2     
+ Partials        2        1    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sushichan044
Copy link
Contributor Author

sushichan044 commented Nov 15, 2024

I used the following code to investigate how much type calculation costs vary.
( Method is the same as ci / perf-measures-type-check )

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 })
  }
)

@sushichan044
Copy link
Contributor Author

sushichan044 commented Nov 15, 2024

The result is as follows.
I think this is an acceptable increase in type calculation cost.

		 Before      After
Files:              257        257
Lines:           114406     114406
Identifiers:     112267     113315
Symbols:         206710     274767
Types:           125123     135373
Instantiations:  492334     548281
Memory used:    271995K    317189K
I/O read:         0.02s      0.02s
I/O write:        0.00s      0.00s
Parse time:       0.49s      0.49s
Bind time:        0.18s      0.19s
Check time:       2.31s      2.52s
Emit time:        0.00s      0.00s
Total time:       2.98s      3.19s

@yusukebe
Copy link
Member

@sushichan044 Thank you for handling it.

Exactly. I also thought the decrease in performance would possibly be a pain point.

I think this is an acceptable increase in type calculation cost.

@m-shaka What do you think of it?

@sushichan044 sushichan044 marked this pull request as ready for review November 28, 2024 17:39
@yusukebe
Copy link
Member

@sushichan044

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.

@sushichan044
Copy link
Contributor Author

sushichan044 commented Nov 29, 2024

@yusukebe

Yes, the experience gained, and performance degraded by this PR should be discussed in depth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type inference breaks in middleware chain with more than two middlewares
2 participants