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

refactor(types): optimize inferring handler types #2412

Merged
merged 2 commits into from
Mar 25, 2024
Merged

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Mar 25, 2024

I've refactored the code for some matters about types.

  • Fixed the handlers' overload order. This improves performance when inferring handler return types.
  • Fixed some bugs for types.
  • Apply Prettify.
  • Fixed tsc errors.

Related to #2399

Performance improvement

Getting faster inferring handler return types. Below are the results if it's a simple case that it has one handler.

Before:

Screenshot 2024-03-25 at 16 16 31

This PR:

Screenshot 2024-03-25 at 16 15 59

This improves RPC-mode performance but does not change much because the implementation of the hc function is not so good.

The scripts for measuring

// create.ts
import { writeFileSync } from 'fs'

let code = `
import { Hono } from 'hono'

const app = new Hono()

const routes = app
`
for (let i = 1; i < 300; i++) {
  code += `.get('/route${i}', (c) => {
    return c.json({
      ok: true
    })
  })`
}

code += `

export type AppType = typeof routes
export default routes
`

writeFileSync(import.meta.dir + '/index.ts', code)
// client.ts
import { hc } from 'hono/client'
import { AppType } from '.'

const client = hc<AppType>('/')

Author should do the followings, if applicable

  • Add tests
  • Run tests
  • yarn denoify to generate files for Deno

@yusukebe yusukebe merged commit 5be4699 into main Mar 25, 2024
10 checks passed
@yusukebe yusukebe deleted the refactor/types branch March 25, 2024 07:49
@KaelWD
Copy link
Contributor

KaelWD commented Jun 27, 2024

@yusukebe this breaks #2019 again

@yusukebe
Copy link
Member Author

@KaelWD

Oops. Fixed with #3051. Thank you!

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.

None yet

2 participants