Skip to content

Commit

Permalink
fix(types): fix the type error in MiddlewareHandlerInterface (#1449)
Browse files Browse the repository at this point in the history
* fix(types): fix the type error in `MiddlewareHandlerInterface`

* denoify
  • Loading branch information
yusukebe committed Sep 12, 2023
1 parent 00af2a5 commit 9cb6b37
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class Hono<
}

// Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])
this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.use = (arg1: string | MiddlewareHandler<any>, ...handlers: MiddlewareHandler<any>[]) => {
if (typeof arg1 === 'string') {
this.path = arg1
} else {
Expand Down
11 changes: 5 additions & 6 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type Handler<
R extends HandlerResponse<any> = any
> = (c: Context<E, P, I>, next: Next) => R

export type MiddlewareHandler<E extends Env = any, P extends string = any, I extends Input = {}> = (
export type MiddlewareHandler<E extends Env = {}, P extends string = any, I extends Input = {}> = (
c: Context<E, P, I>,
next: Next
) => Promise<Response | void>
Expand Down Expand Up @@ -296,11 +296,10 @@ export interface MiddlewareHandlerInterface<
//// app.get(...handlers[])
(...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>
//// app.get(path, ...handlers[])
<P extends string>(path: P, ...handlers: MiddlewareHandler<E, MergePath<BasePath, P>>[]): Hono<
E,
S,
BasePath
>
<P extends string, E2 extends Env = E>(
path: P,
...handlers: MiddlewareHandler<E2, MergePath<BasePath, P>>[]
): Hono<E, S, BasePath>
}

////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ class Hono<
}

// Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])
this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.use = (arg1: string | MiddlewareHandler<any>, ...handlers: MiddlewareHandler<any>[]) => {
if (typeof arg1 === 'string') {
this.path = arg1
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2660,4 +2660,15 @@ describe('c.var - with testing types', () => {
})
}).toThrow()
})

it('Should not throw a type error', (c) => {
const app = new Hono<{
Bindings: {
TOKEN: string
}
}>()
app.get('/', poweredBy(), async (c) => {
type verify = Expect<Equal<string, typeof c.env.TOKEN>>
})
})
})
11 changes: 5 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type Handler<
R extends HandlerResponse<any> = any
> = (c: Context<E, P, I>, next: Next) => R

export type MiddlewareHandler<E extends Env = any, P extends string = any, I extends Input = {}> = (
export type MiddlewareHandler<E extends Env = {}, P extends string = any, I extends Input = {}> = (
c: Context<E, P, I>,
next: Next
) => Promise<Response | void>
Expand Down Expand Up @@ -296,11 +296,10 @@ export interface MiddlewareHandlerInterface<
//// app.get(...handlers[])
(...handlers: MiddlewareHandler<E, MergePath<BasePath, ExtractKey<S>>>[]): Hono<E, S, BasePath>
//// app.get(path, ...handlers[])
<P extends string>(path: P, ...handlers: MiddlewareHandler<E, MergePath<BasePath, P>>[]): Hono<
E,
S,
BasePath
>
<P extends string, E2 extends Env = E>(
path: P,
...handlers: MiddlewareHandler<E2, MergePath<BasePath, P>>[]
): Hono<E, S, BasePath>
}

////////////////////////////////////////
Expand Down

0 comments on commit 9cb6b37

Please sign in to comment.