honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.82k stars 534 forks source link

`ctx.req.query()` should return `Record<string, string | undefined>` #3426

Open cesaryuan opened 5 days ago

cesaryuan commented 5 days ago

What version of Hono are you using?

4.6.2

What runtime/platform is your app running on?

Not matter

What steps can reproduce the bug?

I think the return type of c.req.query() should be Record<string, string | undefined> instead of Record<string, string>

The latter will cause the following code to compile without errors, but to actually run with errors:

const { name } = c.req.query();
return c.text(name.toUpperCase());

What is the expected behavior?

The return type of c.req.query() should be Record<string, string | undefined>

What do you see instead?

Record<string, string>

Additional information

No response

yusukebe commented 5 days ago

Hi @cesaryuan

This is like a bug, but it is a known issue ( sorry for not writing it explicitly). I also think this should be Record<string, string | undefined>. But the change will be breaking change. So, it will be introduced in the next major or minor versions with the announcement. I've left this issue as an enhancement and will consider it. Thank you!

nakasyou commented 3 days ago

Or, you can make noUncheckedIndexedAccess true in tsconfig.json. By enable this, to access indexable object such as Record, Array have to be checked.