Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(hono): fix TS2345 type error in validator.ts#2878

Merged
soartec-lab merged 2 commits intoorval-labs:masterfrom
soartec-lab:fix/hono-type-error
Jan 29, 2026
Merged

fix(hono): fix TS2345 type error in validator.ts#2878
soartec-lab merged 2 commits intoorval-labs:masterfrom
soartec-lab:fix/hono-type-error

Conversation

@soartec-lab
Copy link
Member

fix #2865

There was a type error in validator.ts, so I fixed it. This works without any errors in "@hono/zod-validator": "^0.7.6".

@soartec-lab soartec-lab marked this pull request as ready for review January 29, 2026 13:51
Copilot AI review requested due to automatic review settings January 29, 2026 13:51
@soartec-lab soartec-lab added bug Something isn't working hono Hono related issue labels Jan 29, 2026
@soartec-lab soartec-lab added this to the 8.2.0 milestone Jan 29, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeScript type error (TS2345) in the zValidator function across multiple files by refactoring how the validator handles type conversions when working with @hono/zod-validator v0.7.6.

Changes:

  • Replaced direct type parameter passing with intermediate variables and type assertions
  • Introduced wrapper functions for hook handling to manage type compatibility
  • Added explicit type casting for context and return values

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/hono/src/zValidator.ts Core implementation of the type error fix in the main zValidator package
samples/hono/hono-with-zod/src/petstore.validator.ts Applied the same type error fix to the hono-with-zod sample
samples/hono/hono-with-fetch-client/hono-app/src/petstore.validator.ts Applied the same type error fix to the fetch-client sample
samples/hono/composite-routes-with-tags-split/src/endpoints/validator.ts Applied the same type error fix to the composite-routes sample

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +80 to +84
) =>
hook(
{ ...result, target } as Parameters<typeof hook>[0],
ctx as unknown as Context<E, P>,
)
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type assertion as Parameters<typeof hook>[0] is used to force type compatibility. This approach bypasses TypeScript's type safety and may hide actual type mismatches. Consider refactoring the types to align naturally without requiring this assertion, or add a comment explaining why this assertion is necessary.

Suggested change
) =>
hook(
{ ...result, target } as Parameters<typeof hook>[0],
ctx as unknown as Context<E, P>,
)
) => {
// Adapt the base hook result shape to the user-provided Hook's expected first argument.
type HookFirstArg<TValue, TEnv, TPath, TTarget, TSchema> =
Hook<TValue, TEnv, TPath, TTarget, {}, TSchema> extends (arg: infer A, ...rest: any[]) => any
? A
: never;
return hook(
{ ...result, target } as HookFirstArg<InferredValue, E, P, Target, T>,
ctx as unknown as Context<E, P>,
);
}

Copilot uses AI. Check for mistakes.
? zValidatorBase(baseTarget, schema, baseHook)
: zValidatorBase(baseTarget, schema);
return validator(
c as unknown as Context<Env, string>,
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double type assertion as unknown as Context<Env, string> suggests a significant type incompatibility. This pattern is fragile and may break if type definitions change. Consider whether the function signature or generic constraints can be adjusted to avoid this forced casting.

Copilot uses AI. Check for mistakes.
Comment on lines +77 to +92
Hook<zInfer<T>, Env, string, keyof ValidationTargets, {}, T>
>[0],
ctx: Context<Env, string>,
) =>
hook(
{ ...result, target } as Parameters<typeof hook>[0],
ctx as unknown as Context<E, P>,
)
: undefined;
const validator = baseHook
? zValidatorBase(baseTarget, schema, baseHook)
: zValidatorBase(baseTarget, schema);
return validator(
c as unknown as Context<Env, string>,
next,
) as ReturnType<MiddlewareHandler<E, P, V>>;
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another type assertion is required for the return value, indicating the validator's return type doesn't naturally match the expected type. This creates a chain of type assertions that could mask runtime issues. Consider whether the generic type parameters can be better constrained to eliminate this assertion.

Suggested change
Hook<zInfer<T>, Env, string, keyof ValidationTargets, {}, T>
>[0],
ctx: Context<Env, string>,
) =>
hook(
{ ...result, target } as Parameters<typeof hook>[0],
ctx as unknown as Context<E, P>,
)
: undefined;
const validator = baseHook
? zValidatorBase(baseTarget, schema, baseHook)
: zValidatorBase(baseTarget, schema);
return validator(
c as unknown as Context<Env, string>,
next,
) as ReturnType<MiddlewareHandler<E, P, V>>;
Hook<zInfer<T>, E, P, keyof ValidationTargets, {}, T>
>[0],
ctx: Context<E, P>,
) =>
hook(
{ ...result, target } as Parameters<typeof hook>[0],
ctx,
)
: undefined;
const validator: MiddlewareHandler<E, P, V> = baseHook
? zValidatorBase<zInfer<T>, E, P, V>(baseTarget, schema, baseHook)
: zValidatorBase<zInfer<T>, E, P, V>(baseTarget, schema);
return validator(
c as unknown as Context<E, P>,
next,
);

Copilot uses AI. Check for mistakes.
@soartec-lab soartec-lab merged commit 0ab4749 into orval-labs:master Jan 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hono Hono related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hono: v8 regression, type error in validator.ts

2 participants

Comments