hono icon indicating copy to clipboard operation
hono copied to clipboard

Deeply nested route grouping with regexp breaks client param type inference

Open NuroDev opened this issue 2 years ago • 0 comments

What version of Hono are you using?

4.0.4

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

TypeScript Playground

import { Hono } from 'hono';
import { hc } from 'hono/client';

const foo = new Hono().route(
  '/',
  new Hono().get('/:foo{[a-z]+}', (c) => c.json({ ok: true })),
);
hc<typeof foo>('/')[':foo{[a-z]+}'].$get({ param: {} });
//                                            ^? { foo: string }

const bar = new Hono().route(
  '/',
  new Hono().route(
    '/:foo{[a-z]+}',
    new Hono().get('/', (c) => c.json({ ok: true })),
  ),
);
hc<typeof bar>('/')[':foo{[a-z]+}'].$get({ param: {} });
//                                            ^? { ":foo{[a-z]+}": string }

What is the expected behavior?

Using the example above, ideally the a fix for the types such that the param's required for the client correct strip out the regex pattern.

Testing this locally it is just a case of the types being wrong / broken. If you try to pass in the property with the regex the request will fail. But if you ignore the type error & pass in what should be the correct property, in this case foo, it works just fine.

What do you see instead?

No response

Additional information

No response

NuroDev avatar Feb 22 '24 10:02 NuroDev