Skip to content

fix(zod-openapi): replace any type parameters in OpenAPIHonoOptions.defaultHook#1930

Open
Wajkie wants to merge 1 commit into
honojs:mainfrom
Wajkie:fix/zod-openapi-defaulthook-any-types
Open

fix(zod-openapi): replace any type parameters in OpenAPIHonoOptions.defaultHook#1930
Wajkie wants to merge 1 commit into
honojs:mainfrom
Wajkie:fix/zod-openapi-defaulthook-any-types

Conversation

@Wajkie

@Wajkie Wajkie commented Jun 5, 2026

Copy link
Copy Markdown

Fix: Replace any type parameters in OpenAPIHonoOptions.defaultHook

Problem

OpenAPIHonoOptions types defaultHook as Hook<any, E, any, any>:

export type OpenAPIHonoOptions<E extends Env> = {
  defaultHook?: Hook<any, E, any, any>
}

This leaks any into the c parameter of every defaultHook callback, even when OpenAPIHono is fully parameterized with a concrete Env. The Context<E, P> inside Hook resolves to Context<any, any>, which defeats strict type checking and causes failures with tools like type-coverage --strict.

Root cause

Hook is correctly typed:

export type Hook<T, E extends Env, P extends string, R> = (
  result: ...,
  c: Context<E, P>
) => R

But the three any defaults passed at the call site undo that precision.

Fix

export type OpenAPIHonoOptions<E extends Env> = {
  defaultHook?: Hook<unknown, E, string, Response | void | Promise<Response | void>>
}
  • T: unknowndefaultHook is a catch-all that applies to all routes regardless of their specific input shape. unknown is the correct type when the data type cannot be known at this level (as opposed to any, which silently opts out of type checking).
  • P: string — a route path is always a string. Using any here causes c.req and related path-typed members to resolve to any.
  • R: Response | void | Promise<Response | void> — these are the only meaningful return types for a hook. any allows returning anything, including values that would be silently ignored.

Impact

Before this change, users who set defaultHook on a fully typed OpenAPIHono<BlankEnv> instance still get c: Context<any, any> in the callback. After this change, c is correctly inferred as Context<BlankEnv, string> without any annotation required.

No behaviour changes — runtime is unaffected.

@changeset-bot

changeset-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 162aa07

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

1 participant