diff --git a/src/types.test.ts b/src/types.test.ts index edf3e169..784e2418 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -1776,6 +1776,46 @@ describe('Env types with chained routes - test only types', () => { }) }) +/** + * Ref: https://github.com/honojs/hono/issues/3027 + */ +describe('Env types with validator as first middleware - test only types', () => { + const app = new Hono<{ Variables: { testVar: string } }>() + it('Should not throw a type error', () => { + const testApp = app.get( + validator('json', () => { + return { + cd: 'bar', + } + }), + async (c) => { + const foo = c.req.valid('json') // Error here + return c.json(1) + } + ) + + const dummyMiddleware1 = createMiddleware(async (c, next) => { + await next() + }) + // Multiple levels of middleware + const testApp2 = app.post( + validator('json', () => { + return { + cd: 'bar', + } + }), + dummyMiddleware1, + createMiddleware(async (c, next) => { + await next() + }), + async (c) => { + const foo = c.req.valid('json') // Error here also + return c.json(1) + } + ) + }) +}) + describe('Env types with `use` middleware - test only types', () => { const app = new Hono() diff --git a/src/types.ts b/src/types.ts index 670892e5..bcb378e8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -121,18 +121,6 @@ export interface HandlerInterface< handler: H ): Hono, S & ToSchema>, BasePath> - // app.get(path, handler) - < - P extends string, - MergedPath extends MergePath = MergePath, - R extends HandlerResponse = any, - I extends Input = BlankInput, - E2 extends Env = E - >( - path: P, - handler: H - ): Hono, I, MergeTypedResponse>, BasePath> - // app.get(handler x2) < P extends string = ExtractKey extends never ? BasePath : ExtractKey, @@ -149,19 +137,17 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x2) + // app.get(path, handler) < P extends string, MergedPath extends MergePath = MergePath, R extends HandlerResponse = any, I extends Input = BlankInput, - I2 extends Input = I, - E2 extends Env = E, - E3 extends Env = IntersectNonAnyTypes<[E, E2]> + E2 extends Env = E >( path: P, - ...handlers: [H, H] - ): Hono, I2, MergeTypedResponse>, BasePath> + handler: H + ): Hono, I, MergeTypedResponse>, BasePath> // app.get(handler x 3) < @@ -181,21 +167,19 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x3) + // app.get(path, handler x2) < P extends string, MergedPath extends MergePath = MergePath, R extends HandlerResponse = any, I extends Input = BlankInput, I2 extends Input = I, - I3 extends Input = I & I2, E2 extends Env = E, - E3 extends Env = E, - E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]> + E3 extends Env = IntersectNonAnyTypes<[E, E2]> >( path: P, - ...handlers: [H, H, H] - ): Hono, I3, MergeTypedResponse>, BasePath> + ...handlers: [H, H] + ): Hono, I2, MergeTypedResponse>, BasePath> // app.get(handler x 4) < @@ -217,6 +201,44 @@ export interface HandlerInterface< BasePath > + // app.get(path, handler x3) + < + P extends string, + MergedPath extends MergePath = MergePath, + R extends HandlerResponse = any, + I extends Input = BlankInput, + I2 extends Input = I, + I3 extends Input = I & I2, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]> + >( + path: P, + ...handlers: [H, H, H] + ): Hono, I3, MergeTypedResponse>, BasePath> + + // app.get(handler x 5) + < + P extends string = ExtractKey extends never ? BasePath : ExtractKey, + R extends HandlerResponse = any, + I extends Input = BlankInput, + I2 extends Input = I, + I3 extends Input = I & I2, + I4 extends Input = I & I2 & I3, + I5 extends Input = I & I2 & I3 & I4, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]> + >( + ...handlers: [H, H, H, H, H] + ): Hono< + IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>, + S & ToSchema>, + BasePath + > + // app.get(path, handler x4) < P extends string, @@ -240,54 +262,6 @@ export interface HandlerInterface< ] ): Hono, I4, MergeTypedResponse>, BasePath> - // app.get(handler x 5) - < - P extends string = ExtractKey extends never ? BasePath : ExtractKey, - R extends HandlerResponse = any, - I extends Input = BlankInput, - I2 extends Input = I, - I3 extends Input = I & I2, - I4 extends Input = I & I2 & I3, - I5 extends Input = I & I2 & I3 & I4, - E2 extends Env = E, - E3 extends Env = E, - E4 extends Env = E, - E5 extends Env = E, - E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]> - >( - ...handlers: [H, H, H, H, H] - ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>, - S & ToSchema>, - BasePath - > - - // app.get(path, handler x5) - < - P extends string, - MergedPath extends MergePath = MergePath, - R extends HandlerResponse = any, - I extends Input = BlankInput, - I2 extends Input = I, - I3 extends Input = I & I2, - I4 extends Input = I & I2 & I3, - I5 extends Input = I & I2 & I3 & I4, - E2 extends Env = E, - E3 extends Env = E, - E4 extends Env = E, - E5 extends Env = E, - E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]> - >( - path: P, - ...handlers: [ - H, - H, - H, - H, - H - ] - ): Hono, I5, MergeTypedResponse>, BasePath> - // app.get(handler x 6) < P extends string = ExtractKey extends never ? BasePath : ExtractKey, @@ -319,7 +293,7 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x6) + // app.get(path, handler x5) < P extends string, MergedPath extends MergePath = MergePath, @@ -329,13 +303,11 @@ export interface HandlerInterface< I3 extends Input = I & I2, I4 extends Input = I & I2 & I3, I5 extends Input = I & I2 & I3 & I4, - I6 extends Input = I & I2 & I3 & I4 & I5, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = E, - E6 extends Env = E, - E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]> + E6 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5]> >( path: P, ...handlers: [ @@ -343,10 +315,9 @@ export interface HandlerInterface< H, H, H, - H, - H + H ] - ): Hono, I6, MergeTypedResponse>, BasePath> + ): Hono, I5, MergeTypedResponse>, BasePath> // app.get(handler x 7) < @@ -382,7 +353,7 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x7) + // app.get(path, handler x6) < P extends string, MergedPath extends MergePath = MergePath, @@ -393,14 +364,12 @@ export interface HandlerInterface< I4 extends Input = I & I2 & I3, I5 extends Input = I & I2 & I3 & I4, I6 extends Input = I & I2 & I3 & I4 & I5, - I7 extends Input = I & I2 & I3 & I4 & I5 & I6, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = E, E6 extends Env = E, - E7 extends Env = E, - E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]> + E7 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]> >( path: P, ...handlers: [ @@ -409,10 +378,9 @@ export interface HandlerInterface< H, H, H, - H, - H + H ] - ): Hono, I7, MergeTypedResponse>, BasePath> + ): Hono, I6, MergeTypedResponse>, BasePath> // app.get(handler x 8) < @@ -451,7 +419,7 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x8) + // app.get(path, handler x7) < P extends string, MergedPath extends MergePath = MergePath, @@ -463,15 +431,13 @@ export interface HandlerInterface< I5 extends Input = I & I2 & I3 & I4, I6 extends Input = I & I2 & I3 & I4 & I5, I7 extends Input = I & I2 & I3 & I4 & I5 & I6, - I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, E5 extends Env = E, E6 extends Env = E, E7 extends Env = E, - E8 extends Env = E, - E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]> + E8 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]> >( path: P, ...handlers: [ @@ -481,10 +447,9 @@ export interface HandlerInterface< H, H, H, - H, - H + H ] - ): Hono, I8, MergeTypedResponse>, BasePath> + ): Hono, I7, MergeTypedResponse>, BasePath> // app.get(handler x 9) < @@ -526,7 +491,7 @@ export interface HandlerInterface< BasePath > - // app.get(path, handler x9) + // app.get(path, handler x8) < P extends string, MergedPath extends MergePath = MergePath, @@ -539,7 +504,6 @@ export interface HandlerInterface< I6 extends Input = I & I2 & I3 & I4 & I5, I7 extends Input = I & I2 & I3 & I4 & I5 & I6, I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7, - I9 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7 & I8, E2 extends Env = E, E3 extends Env = E, E4 extends Env = E, @@ -547,8 +511,7 @@ export interface HandlerInterface< E6 extends Env = E, E7 extends Env = E, E8 extends Env = E, - E9 extends Env = E, - E10 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]> + E9 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]> >( path: P, ...handlers: [ @@ -559,10 +522,9 @@ export interface HandlerInterface< H, H, H, - H, - H + H ] - ): Hono, I9, MergeTypedResponse>, BasePath> + ): Hono, I8, MergeTypedResponse>, BasePath> // app.get(handler x 10) < @@ -607,6 +569,44 @@ export interface HandlerInterface< BasePath > + // app.get(path, handler x9) + < + P extends string, + MergedPath extends MergePath = MergePath, + R extends HandlerResponse = any, + I extends Input = BlankInput, + I2 extends Input = I, + I3 extends Input = I & I2, + I4 extends Input = I & I2 & I3, + I5 extends Input = I & I2 & I3 & I4, + I6 extends Input = I & I2 & I3 & I4 & I5, + I7 extends Input = I & I2 & I3 & I4 & I5 & I6, + I8 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7, + I9 extends Input = I & I2 & I3 & I4 & I5 & I6 & I7 & I8, + E2 extends Env = E, + E3 extends Env = E, + E4 extends Env = E, + E5 extends Env = E, + E6 extends Env = E, + E7 extends Env = E, + E8 extends Env = E, + E9 extends Env = E, + E10 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]> + >( + path: P, + ...handlers: [ + H, + H, + H, + H, + H, + H, + H, + H, + H + ] + ): Hono, I9, MergeTypedResponse>, BasePath> + // app.get(path, handler x10) < P extends string,