honojs / hono

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

4.4.7 broke array types in Context #3131

Closed dustinsmith1024 closed 1 month ago

dustinsmith1024 commented 2 months ago

What version of Hono are you using?

4.4.7+

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

I am attempting to upgrade to the latest 4.4 versions and I get a types issue with Array based elements in the HonoContext.

Sample context:

export interface DC {
  name: string;
  url: string;
}

declare module 'hono' {
  interface ContextVariableMap {
    jwt: JWTInterface;
    dcs: DC[];
    allowAllOrigins: boolean;
    allowedOrigins: string[];
    appToken: string;
    appEnv: string;
  }
}

What is the expected behavior?

No type errors should happen. This doesn't happen on 4.4.6, so maybe related to https://github.com/honojs/hono/pull/2987?

What do you see instead?

➜ pnpm run -F gateway-router tsc

> router@0.1.0 tsc router
> tsc --noEmit                                 ~~

src/file.ts:327:16 - error TS7006: Parameter 'dc' implicitly has an 'any' type.

327       .filter((dc) => userDcs.includes(dc.name))
                   ~~

src/file.ts:328:13 - error TS7006: Parameter 'dc' implicitly has an 'any' type.

328       .map((dc) => {
                ~~

src/file.ts:50:40 - error TS7006: Parameter 'dc' implicitly has an 'any' type.

50     if (!body.dc || c.get('dcs').find((dc) => dc.name === body.dc) === undefined) {
                                          ~~

src/file2.ts:180:55 - error TS7006: Parameter 'allowed' implicitly has an 'any' type.

180         const allowed = c.get('allowedOrigins').find((allowed) => origin === allowed);
                                                          ~~~~~~~

Additional information

No response

yusukebe commented 2 months ago

Hi @dustinsmith1024

Is this way of reproducing it correctly?

https://github.com/user-attachments/assets/52a1fe57-c3a1-434e-a64c-a8c6976ba9a0

If so, it does not throw errors. Can you provide a minimal project to reproduce it?

dustinsmith1024 commented 2 months ago

Here you go: https://github.com/dustinsmith1024/hono-types-issue

yusukebe commented 2 months ago

@dustinsmith1024 Thanks. Will be fixed soon.

dustinsmith1024 commented 2 months ago

Awesome thanks - Should I be doing something different with Context to prevent it?

yusukebe commented 2 months ago

@dustinsmith1024

Should I be doing something different with Context to prevent it?

It's a bug, so you don't have to add anything. Just wait for a new version!

yusukebe commented 1 month ago

Hi @dustinsmith1024

This may be fixed in the latest version v4.5.0. Can you try it?

yusukebe commented 1 month ago

We can close this issue now. If you still have a problem, feel free to reopen.