Skip to content

fix(validator): preserve literal union types in input type inference#4583

Merged
yusukebe merged 6 commits into
mainfrom
fix/validator-union-types-input
Dec 18, 2025
Merged

fix(validator): preserve literal union types in input type inference#4583
yusukebe merged 6 commits into
mainfrom
fix/validator-union-types-input

Conversation

@yusukebe

@yusukebe yusukebe commented Dec 17, 2025

Copy link
Copy Markdown
Member

Related to #2481 (comment) #4584

This PR will fix the issue where the query input type is incorrectly typed when it has a union or an optional.

import { Hono } from 'hono'
import { validator } from 'hono/validator'
import { hc } from 'hono/client'

const app = new Hono()

const routes = app.get(
  '/',
  validator('query', () => {
    return {
      page: 1,
      orderBy: 'asc',
    } as {
      page: number
      orderBy: 'asc' | 'desc'
      ordreByWithdefault?: 'asc' | 'desc' | undefined
    }
  }),
  (c) => {
    const data = c.req.valid('query')
    return c.json(data)
  }
)

const client = hc<typeof routes>('http://localhost:8787')

client.index.$get({
  query: {
    page: '1', // string | string[]
    orderBy: 'asc', // ❌️ string | string[] => should be ⭕️ 'asc' | 'desc'
    ordreByWithdefault: 'asc', // ❌️ string | string[] | undefined => should be ⭕️ 'asc' | 'desc' | undefined
  },
})

To make it easier, I created an InferInput type in utils.ts. If you want to fix @hono/zod-validator or another module, you can copy the InferInput code from the file. If so, you don't have to update the hono version in peerDependencies.

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

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@codecov

codecov Bot commented Dec 17, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.56%. Comparing base (d3abeb1) to head (665a383).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4583   +/-   ##
=======================================
  Coverage   91.56%   91.56%           
=======================================
  Files         172      173    +1     
  Lines       11255    11256    +1     
  Branches     3265     3266    +1     
=======================================
+ Hits        10306    10307    +1     
  Misses        948      948           
  Partials        1        1           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@pkg-pr-new

pkg-pr-new Bot commented Dec 18, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/hono@4583

commit: 665a383

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown

Bundle size check

main (d3abeb1) #4583 (dbfc962) +/-
Bundle Size (B) 18,377B 18,377B 0B
Bundle Size (KB) 17.95K 17.95K 0K

Compiler Diagnostics (tsc)

main (d3abeb1) #4583 (dbfc962) +/-
Files 303 303 0
Lines 141,168 141,168 0
Identifiers 128,951 128,951 0
Symbols 269,872 269,872 0
Types 206,566 206,566 0
Instantiations 666,443 666,443 0
Memory used 346,343K 353,552K 7,209K
I/O read 0.02s 0.02s 0s
I/O write 0s 0s 0s
Parse time 0.71s 0.7s -0.01s
Bind time 0.3s 0.29s -0.01s
Check time 2.51s 2.51s 0s
Emit time 0s 0s 0s
Total time 3.52s 3.5s -0.02s

Compiler Diagnostics (typescript-go)

main (d3abeb1) #4583 (dbfc962) +/-
Files 272 272 0
Lines 119,057 119,057 0
Identifiers 118,384 118,384 0
Symbols 379,638 379,638 0
Types 375,200 375,200 0
Instantiations 3,857,783 3,857,783 0
Memory used 248,136K 248,143K 7K
Memory allocs 10,480,388 10,480,269 -119
Parse time 0.079s 0.076s -0.003s
Bind time 0.017s 0.016s -0.001s
Check time 1.327s 1.387s 0.06s
Emit time 0.001s 0.001s 0s
Total time 1.425s 1.482s 0.057s

Reported by octocov

@github-actions

Copy link
Copy Markdown

HTTP Performance Benchmark

Framework Runtime Average Ping Query Body
hono (origin/main) bun 52,235.56 70,140.42 47,309.68 39,256.59
hono (current) bun 52,058.62 68,834.57 47,660.94 39,680.36
Change -0.34% -1.86% +0.74% +1.08%

@yusukebe yusukebe merged commit bde9a03 into main Dec 18, 2025
21 checks passed
@yusukebe yusukebe deleted the fix/validator-union-types-input branch December 18, 2025 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant