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

TypeScript Error TS2345: Argument of type 'T' is not assignable to parameter of type 'ZodType<any, ZodTypeDef, any>' #982

Open
Mudong-17 opened this issue Feb 26, 2025 · 1 comment
Labels

Comments

@Mudong-17
Copy link

Which middleware has the bug?

@hono/zod-validator

What version of the middleware?

0.4.3

What version of Hono are you using?

4.7.2

What runtime/platform is your app running on? (with version if possible)

bun

What steps can reproduce the bug?

I'm encountering a TypeScript error when attempting to use the zValidator function from the @hono/zod-validator package. The error message is as follows:

Argument of type 'T' is not assignable to parameter of type 'ZodType<any, ZodTypeDef, any>'.
  Type 'ZodType<any, ZodTypeDef, any>' is missing the following properties from type 'ZodType<any, ZodTypeDef, any>': "~standard", "~validate"ts(2345)
(parameter) schema: T extends ZodSchema

Code Example:

import { ZodSchema } from "zod";
import type { ValidationTargets } from "hono";
import { zValidator as zv } from "@hono/zod-validator";

export const zValidator = <
  T extends ZodSchema,
  Target extends keyof ValidationTargets
>(
  target: Target,
  schema: T
) =>
  zv(target, schema, (result, c) => {
    if (!result.success) {
      throw new HTTPException(400, { cause: result.error });
    }
  });

tsconfig

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "strict": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,

    "esModuleInterop": true,
    "isolatedModules": true,


    "allowJs": true,
    "skipLibCheck": true,
    "noEmit": true,
    "experimentalDecorators": true,
    "target": "ESNext"
  }
}

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@yusukebe
Copy link
Member

@Mudong-17

We don't expect to use that syntax and don't support it. Please use it as written in the README.

import { z } from 'zod'
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'

const app = new Hono()

const schema = z.object({})

app.get('/', zValidator('json', schema), (c) => {
  return c.json({})
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants