fix(hono): fix TS2345 type error in validator.ts#2878
fix(hono): fix TS2345 type error in validator.ts#2878soartec-lab merged 2 commits intoorval-labs:masterfrom
TS2345 type error in validator.ts#2878Conversation
There was a problem hiding this comment.
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.
| ) => | ||
| hook( | ||
| { ...result, target } as Parameters<typeof hook>[0], | ||
| ctx as unknown as Context<E, P>, | ||
| ) |
There was a problem hiding this comment.
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.
| ) => | |
| 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>, | |
| ); | |
| } |
| ? zValidatorBase(baseTarget, schema, baseHook) | ||
| : zValidatorBase(baseTarget, schema); | ||
| return validator( | ||
| c as unknown as Context<Env, string>, |
There was a problem hiding this comment.
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.
| 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>>; |
There was a problem hiding this comment.
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.
| 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, | |
| ); |
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".