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

npm:@hono/typebox-validator imports @sinclair/typebox as cjs, while direct import "npm:@sinclair/typebox" imports esm, leading to type mismatch #27754

Closed
cfraz89 opened this issue Jan 21, 2025 · 1 comment

Comments

@cfraz89
Copy link

cfraz89 commented Jan 21, 2025

Version: Deno 2.1.6

Attempting to write a server using hono, @hono/typebox-validator and @sinclair/typebox packages all installed from npm:

import { Hono } from 'hono'
import { tbValidator } from '@hono/typebox-validator'
import { Type as T, TSchema } from '@sinclair/typebox'
  
const app = new Hono()

const schema: TSchema = T.Object({ request: T.String() })

app.post('/route', tbValidator('json', schema),  async (c) => {
  const request = c.req.valid('json')
  return c.json({ request })
})

Deno.serve(app.fetch)

Results in the following error on compile:

> deno check main.ts

Check file:///var/home/chris/Projects/expedition-hono/main.ts
error: TS2345 [ERROR]: Argument of type 'import("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/esm/type/schema/schema.d.mts").TSchema' is not assignable to parameter of type 'import("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/cjs/type/schema/schema.d.ts").TSchema'.
  Property '[Kind]' is missing in type 'import("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/esm/type/schema/schema.d.mts").TSchema' but required in type 'import("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/cjs/type/schema/schema.d.ts").TSchema'.
app.post('/route', tbValidator('json', schema),  async (c) => {
                                       ~~~~~~
    at file:///var/home/chris/Projects/expedition-hono/main.ts:9:40

    '[Kind]' is declared here.
        [Kind]: string;
        ~~~~~~
        at file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/cjs/type/schema/schema.d.ts:21:5

It's complaining that TSchema type is mismatched between esm and cjs imports.

Running with `--log-level=debug`, we can see where it breaks down :

DEBUG RS - deno_resolver::npm::managed:185 - Resolved package folder of @sinclair/[email protected] to /var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14
DEBUG RS - deno::tsc:870 - Resolved @sinclair/typebox from file:///var/home/chris/Projects/expedition-hono/main.ts to ("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/esm/index.d.mts", Some(".d.mts"))
...
DEBUG RS - deno_resolver::npm::managed:250 - Resolved @sinclair/typebox from file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@hono/typebox-validator/0.3.0/dist/esm/index.d.ts to /var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14
DEBUG RS - deno::tsc:870 - Resolved @sinclair/typebox from file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@hono/typebox-validator/0.3.0/dist/esm/index.d.ts to ("file:///var/home/chris/.cache/deno/npm/registry.npmjs.org/@sinclair/typebox/0.34.14/build/cjs/index.d.ts", Some(".d.ts"))

So we can see that the bare imports are importing the esm build, but the import from inside @hono/typebox-validator decided to import the cjs build. And since they both define their own symbol of Kind, the types end up mismatching.

I assume the expected behaviour here would be for @sinclair/typebox to resolve to the esm build in both cases, but I don't know the ins and outs of esm/cjs modules that well, so can't say whether it's an issue with the packages, deno's resolution logic, or just the way it is.

Have provided reproduction repo here: https://github.com/cfraz89/hono-typebox-deno-error-demo and attached a log of deno check --log-level=debug main.ts

debug.log

@cfraz89
Copy link
Author

cfraz89 commented Jan 21, 2025

Just realised this occurs in node too, so I'll close the issue and reopen with hono's tracker.

@cfraz89 cfraz89 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
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

No branches or pull requests

1 participant