honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
451 stars 164 forks source link

[zod-validator] Deno infers Context's Input to BlankInput #803

Open bakasmarius opened 3 days ago

bakasmarius commented 3 days ago

Context generics (specifically, Input) are inferred differently in Deno and Node for the same code.

Deno: v1.46.3; v2.0.3; v2.0.4 Node: v20.18.0 Hono: v4.6.8 @hono/zod-validator: v0.4.1 zod: v3.23.8

Node code:

import { Hono } from 'hono'
import { z } from 'zod'

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

const app = new Hono().put(
  '/posts',
  zValidator('json', z.object({ id: z.number() })),
  (c) => {
    const json = c.req.valid('json')
    return c.json({ status: 'ok' })
  }
)

TypeScript infers c to:

(parameter) c: Context<{}, "/posts", {
    in: {
        json: {
            id: number;
        };
    };
    out: {
        json: {
            id: number;
        };
    };
}>

Deno code:

import { z } from 'npm:zod@3.23.8'

import { zValidator } from 'npm:@hono/zod-validator@0.4.1'
import { Hono } from 'npm:hono@4.6.8'

const app = new Hono().put(
  '/posts',
  zValidator('json', z.object({ id: z.number() })),
  (c) => {
    const json = c.req.valid('json')
    return c.json({ status: 'ok' })
  },
)

TypeScript infers c to:

(parameter) c: Context<{}, "/posts", BlankInput>

Which results in c.req.valid('json') error: Argument of type 'string' is not assignable to parameter of type 'never'.deno-ts(2345)

yusukebe commented 2 days ago

Hi @bakasmarius

It is not reproduced. Works well in my environment:

CleanShot 2024-11-04 at 10 47 15@2x

Please clear the cache and try it again:

deno cache --reload main.ts