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

Draft
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 (In progress)
  • 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 (1af264b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3675   +/-   ##
=======================================
  Coverage   91.70%   91.70%           
=======================================
  Files         159      159           
  Lines       10145    10145           
  Branches     2862     2942   +80     
=======================================
  Hits         9303     9303           
  Misses        840      840           
  Partials        2        2           

☔ 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?

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