hono icon indicating copy to clipboard operation
hono copied to clipboard

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

Open cesaryuan opened this issue 1 year ago • 2 comments

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

cesaryuan avatar Sep 20 '24 01:09 cesaryuan

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!

yusukebe avatar Sep 20 '24 10:09 yusukebe

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

nakasyou avatar Sep 22 '24 01:09 nakasyou