From acd53a1725866b05125cc1de8919cb87b321e2c9 Mon Sep 17 00:00:00 2001 From: agatan Date: Thu, 18 Jan 2024 22:33:51 +0900 Subject: [PATCH 1/6] fix(types): Fix handler return types not to leak Context types --- src/hono.test.ts | 11 ++++ src/types.test.ts | 157 +++------------------------------------------- src/types.ts | 20 +++--- 3 files changed, 28 insertions(+), 160 deletions(-) diff --git a/src/hono.test.ts b/src/hono.test.ts index 466f99581..0480981c1 100644 --- a/src/hono.test.ts +++ b/src/hono.test.ts @@ -3134,6 +3134,17 @@ describe('c.var - with testing types', () => { app.use(['foo', 'bar'], poweredBy()) } catch {} }) + + it('Should not throw type errors', async (c) => { + app.get('/chained/1', mw(), (c) => { + return c.text(c.var.echo('hello')) + }).get('/chained/2', (c) => { + expectTypeOf(c.var).not.toHaveProperty('echo') + return c.text('echo' in c.var ? 'echo is undefined. ' : 'echo is defined.') + }) + const resp = await app.request('/chained/3') + expect(await resp.text()).toBe('echo is undefined. ') + }) }) describe('Compatible with extended Hono classes, such Zod OpenAPI Hono.', () => { diff --git a/src/types.test.ts b/src/types.test.ts index b3b4fe08f..86d1ea20c 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -838,156 +838,6 @@ describe('c.var with chaining - test only types', () => { // app.get('/', handler...) - new Hono().get('/', mw1).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.get('foo6')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - expectTypeOf(c.var.foo6).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.get('foo6')).toEqualTypeOf() - expectTypeOf(c.get('foo7')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - expectTypeOf(c.var.foo6).toEqualTypeOf() - expectTypeOf(c.var.foo7).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.get('foo6')).toEqualTypeOf() - expectTypeOf(c.get('foo7')).toEqualTypeOf() - expectTypeOf(c.get('foo8')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - expectTypeOf(c.var.foo6).toEqualTypeOf() - expectTypeOf(c.var.foo7).toEqualTypeOf() - expectTypeOf(c.var.foo8).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.get('foo6')).toEqualTypeOf() - expectTypeOf(c.get('foo7')).toEqualTypeOf() - expectTypeOf(c.get('foo8')).toEqualTypeOf() - expectTypeOf(c.get('foo9')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - expectTypeOf(c.var.foo6).toEqualTypeOf() - expectTypeOf(c.var.foo7).toEqualTypeOf() - expectTypeOf(c.var.foo8).toEqualTypeOf() - expectTypeOf(c.var.foo9).toEqualTypeOf() - return c.json(0) - }) - - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, mw10).get('/', (c) => { - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.get('foo2')).toEqualTypeOf() - expectTypeOf(c.get('foo3')).toEqualTypeOf() - expectTypeOf(c.get('foo4')).toEqualTypeOf() - expectTypeOf(c.get('foo5')).toEqualTypeOf() - expectTypeOf(c.get('foo6')).toEqualTypeOf() - expectTypeOf(c.get('foo7')).toEqualTypeOf() - expectTypeOf(c.get('foo8')).toEqualTypeOf() - expectTypeOf(c.get('foo9')).toEqualTypeOf() - expectTypeOf(c.get('foo10')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - expectTypeOf(c.var.foo2).toEqualTypeOf() - expectTypeOf(c.var.foo3).toEqualTypeOf() - expectTypeOf(c.var.foo4).toEqualTypeOf() - expectTypeOf(c.var.foo5).toEqualTypeOf() - expectTypeOf(c.var.foo6).toEqualTypeOf() - expectTypeOf(c.var.foo7).toEqualTypeOf() - expectTypeOf(c.var.foo8).toEqualTypeOf() - expectTypeOf(c.var.foo9).toEqualTypeOf() - expectTypeOf(c.var.foo10).toEqualTypeOf() - return c.json(0) - }) - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => { expectTypeOf(c.req.valid('query')).toMatchTypeOf<{ bar1: number @@ -1077,3 +927,10 @@ describe('Env types with `use` middleware - test only types', () => { }) }) }) + +describe('Env types with chained routes with middlewares - test only types', () => { + const app = new Hono() + + const mw1 = createMiddleware<{ Variables: { foo1: string } }>(async () => {}) + const mw2 = createMiddleware<{ Variables: { foo2: string } }>(async () => {}) +}) diff --git a/src/types.ts b/src/types.ts index df23cba4e..3316181b7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -395,7 +395,7 @@ export interface HandlerInterface< path: P, handler: H ): Hono< - IntersectNonAnyTypes<[E, E2]>, + E, S & ToSchema, I['in'], MergeTypedResponseData>, BasePath > @@ -413,7 +413,7 @@ export interface HandlerInterface< path: P, ...handlers: [H, H] ): Hono< - IntersectNonAnyTypes<[E, E2, E3]>, + E, S & ToSchema, I2['in'], MergeTypedResponseData>, BasePath > @@ -433,7 +433,7 @@ export interface HandlerInterface< path: P, ...handlers: [H, H, H] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4]>, + E, S & ToSchema, I3['in'], MergeTypedResponseData>, BasePath > @@ -460,7 +460,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5]>, + E, S & ToSchema, I4['in'], MergeTypedResponseData>, BasePath > @@ -490,7 +490,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>, + E, S & ToSchema, I5['in'], MergeTypedResponseData>, BasePath > @@ -523,7 +523,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>, + E, S & ToSchema, I6['in'], MergeTypedResponseData>, BasePath > @@ -559,7 +559,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>, + E, S & ToSchema, I7['in'], MergeTypedResponseData>, BasePath > @@ -598,7 +598,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>, + E, S & ToSchema, I8['in'], MergeTypedResponseData>, BasePath > @@ -640,7 +640,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>, + E, S & ToSchema, I9['in'], MergeTypedResponseData>, BasePath > @@ -685,7 +685,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>, + E, S & ToSchema, I10['in'], MergeTypedResponseData>, BasePath > From 5fdbf01d126d516fb4065f2116d7235d3cf7beab Mon Sep 17 00:00:00 2001 From: agatan Date: Sat, 20 Jan 2024 23:50:38 +0900 Subject: [PATCH 2/6] fix specs --- src/types.test.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/types.test.ts b/src/types.test.ts index 86d1ea20c..a7773855a 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -836,8 +836,6 @@ describe('c.var with chaining - test only types', () => { return c.json(0) }) - // app.get('/', handler...) - new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => { expectTypeOf(c.req.valid('query')).toMatchTypeOf<{ bar1: number @@ -853,6 +851,28 @@ describe('c.var with chaining - test only types', () => { return c.json(0) }) + + type Env = { + Variables: { + init: number + } + } + + new Hono().get('/', mw1, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + return c.json(0) + }).get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + return c.json(0) + }) }) }) From 34d6be2c2e4796a0c2c755417d21fb21665e996f Mon Sep 17 00:00:00 2001 From: agatan Date: Sat, 20 Jan 2024 23:51:23 +0900 Subject: [PATCH 3/6] denoify --- deno_dist/types.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/deno_dist/types.ts b/deno_dist/types.ts index 9d6d44e3b..424e350c9 100644 --- a/deno_dist/types.ts +++ b/deno_dist/types.ts @@ -395,7 +395,7 @@ export interface HandlerInterface< path: P, handler: H ): Hono< - IntersectNonAnyTypes<[E, E2]>, + E, S & ToSchema, I['in'], MergeTypedResponseData>, BasePath > @@ -413,7 +413,7 @@ export interface HandlerInterface< path: P, ...handlers: [H, H] ): Hono< - IntersectNonAnyTypes<[E, E2, E3]>, + E, S & ToSchema, I2['in'], MergeTypedResponseData>, BasePath > @@ -433,7 +433,7 @@ export interface HandlerInterface< path: P, ...handlers: [H, H, H] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4]>, + E, S & ToSchema, I3['in'], MergeTypedResponseData>, BasePath > @@ -460,7 +460,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5]>, + E, S & ToSchema, I4['in'], MergeTypedResponseData>, BasePath > @@ -490,7 +490,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6]>, + E, S & ToSchema, I5['in'], MergeTypedResponseData>, BasePath > @@ -523,7 +523,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7]>, + E, S & ToSchema, I6['in'], MergeTypedResponseData>, BasePath > @@ -559,7 +559,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8]>, + E, S & ToSchema, I7['in'], MergeTypedResponseData>, BasePath > @@ -598,7 +598,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9]>, + E, S & ToSchema, I8['in'], MergeTypedResponseData>, BasePath > @@ -640,7 +640,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10]>, + E, S & ToSchema, I9['in'], MergeTypedResponseData>, BasePath > @@ -685,7 +685,7 @@ export interface HandlerInterface< H ] ): Hono< - IntersectNonAnyTypes<[E, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11]>, + E, S & ToSchema, I10['in'], MergeTypedResponseData>, BasePath > From f93308daf46869382876208dfda09932daa76454 Mon Sep 17 00:00:00 2001 From: agatan Date: Sat, 20 Jan 2024 23:56:51 +0900 Subject: [PATCH 4/6] fix specs --- src/hono.test.ts | 11 ----------- src/types.test.ts | 7 ------- 2 files changed, 18 deletions(-) diff --git a/src/hono.test.ts b/src/hono.test.ts index 0480981c1..466f99581 100644 --- a/src/hono.test.ts +++ b/src/hono.test.ts @@ -3134,17 +3134,6 @@ describe('c.var - with testing types', () => { app.use(['foo', 'bar'], poweredBy()) } catch {} }) - - it('Should not throw type errors', async (c) => { - app.get('/chained/1', mw(), (c) => { - return c.text(c.var.echo('hello')) - }).get('/chained/2', (c) => { - expectTypeOf(c.var).not.toHaveProperty('echo') - return c.text('echo' in c.var ? 'echo is undefined. ' : 'echo is defined.') - }) - const resp = await app.request('/chained/3') - expect(await resp.text()).toBe('echo is undefined. ') - }) }) describe('Compatible with extended Hono classes, such Zod OpenAPI Hono.', () => { diff --git a/src/types.test.ts b/src/types.test.ts index a7773855a..e09d1b3f6 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -947,10 +947,3 @@ describe('Env types with `use` middleware - test only types', () => { }) }) }) - -describe('Env types with chained routes with middlewares - test only types', () => { - const app = new Hono() - - const mw1 = createMiddleware<{ Variables: { foo1: string } }>(async () => {}) - const mw2 = createMiddleware<{ Variables: { foo2: string } }>(async () => {}) -}) From ef3b669a6df075fab467d39a1a0287b6e4cfbcfd Mon Sep 17 00:00:00 2001 From: agatan Date: Sun, 21 Jan 2024 00:26:06 +0900 Subject: [PATCH 5/6] yarm format:fix && yarn denoify --- deno_dist/types.ts | 54 ++++++++-------------------------------------- src/types.test.ts | 34 +++++++++++++++-------------- src/types.ts | 54 ++++++++-------------------------------------- 3 files changed, 36 insertions(+), 106 deletions(-) diff --git a/deno_dist/types.ts b/deno_dist/types.ts index 424e350c9..6f9f6e0fe 100644 --- a/deno_dist/types.ts +++ b/deno_dist/types.ts @@ -394,11 +394,7 @@ export interface HandlerInterface< >( path: P, handler: H - ): Hono< - E, - S & ToSchema, I['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x2) < @@ -412,11 +408,7 @@ export interface HandlerInterface< >( path: P, ...handlers: [H, H] - ): Hono< - E, - S & ToSchema, I2['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I2['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x3) < @@ -432,11 +424,7 @@ export interface HandlerInterface< >( path: P, ...handlers: [H, H, H] - ): Hono< - E, - S & ToSchema, I3['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I3['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x4) < @@ -459,11 +447,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I4['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I4['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x5) < @@ -489,11 +473,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I5['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I5['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x6) < @@ -522,11 +502,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I6['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I6['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x7) < @@ -558,11 +534,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I7['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I7['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x8) < @@ -597,11 +569,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I8['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I8['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x9) < @@ -639,11 +607,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I9['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I9['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x10) < diff --git a/src/types.test.ts b/src/types.test.ts index e09d1b3f6..fef376de7 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -857,22 +857,24 @@ describe('c.var with chaining - test only types', () => { init: number } } - - new Hono().get('/', mw1, (c) => { - expectTypeOf(c.get('init')).toEqualTypeOf() - expectTypeOf(c.var.init).toEqualTypeOf() - expectTypeOf(c.get('foo1')).toEqualTypeOf() - expectTypeOf(c.var.foo1).toEqualTypeOf() - return c.json(0) - }).get('/', (c) => { - expectTypeOf(c.get('init')).toEqualTypeOf() - expectTypeOf(c.var.init).toEqualTypeOf() - // @ts-expect-error foo1 is not typed - c.get('foo1') - // @ts-expect-error foo1 is not typed - c.var.foo1 - return c.json(0) - }) + + new Hono() + .get('/', mw1, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + return c.json(0) + }) }) }) diff --git a/src/types.ts b/src/types.ts index 3316181b7..aa554ef3b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -394,11 +394,7 @@ export interface HandlerInterface< >( path: P, handler: H - ): Hono< - E, - S & ToSchema, I['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x2) < @@ -412,11 +408,7 @@ export interface HandlerInterface< >( path: P, ...handlers: [H, H] - ): Hono< - E, - S & ToSchema, I2['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I2['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x3) < @@ -432,11 +424,7 @@ export interface HandlerInterface< >( path: P, ...handlers: [H, H, H] - ): Hono< - E, - S & ToSchema, I3['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I3['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x4) < @@ -459,11 +447,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I4['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I4['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x5) < @@ -489,11 +473,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I5['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I5['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x6) < @@ -522,11 +502,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I6['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I6['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x7) < @@ -558,11 +534,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I7['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I7['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x8) < @@ -597,11 +569,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I8['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I8['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x9) < @@ -639,11 +607,7 @@ export interface HandlerInterface< H, H ] - ): Hono< - E, - S & ToSchema, I9['in'], MergeTypedResponseData>, - BasePath - > + ): Hono, I9['in'], MergeTypedResponseData>, BasePath> // app.get(path, handler x10) < From f05c32e5f16e04c8ee660959e1abc25d144bb53d Mon Sep 17 00:00:00 2001 From: agatan Date: Tue, 23 Jan 2024 13:39:27 +0900 Subject: [PATCH 6/6] Add tests --- src/types.test.ts | 360 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) diff --git a/src/types.test.ts b/src/types.test.ts index fef376de7..2f64e8544 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -875,6 +875,366 @@ describe('c.var with chaining - test only types', () => { c.var.foo1 return c.json(0) }) + + new Hono() + .get('/', mw1, mw2, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, mw5, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + expectTypeOf(c.get('foo5')).toEqualTypeOf() + expectTypeOf(c.var.foo5).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + // @ts-expect-error foo5 is not typed + c.get('foo5') + // @ts-expect-error foo5 is not typed + c.var.foo5 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, mw5, mw6, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + expectTypeOf(c.get('foo5')).toEqualTypeOf() + expectTypeOf(c.var.foo5).toEqualTypeOf() + expectTypeOf(c.get('foo6')).toEqualTypeOf() + expectTypeOf(c.var.foo6).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + // @ts-expect-error foo5 is not typed + c.get('foo5') + // @ts-expect-error foo5 is not typed + c.var.foo5 + // @ts-expect-error foo6 is not typed + c.get('foo6') + // @ts-expect-error foo6 is not typed + c.var.foo6 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + expectTypeOf(c.get('foo5')).toEqualTypeOf() + expectTypeOf(c.var.foo5).toEqualTypeOf() + expectTypeOf(c.get('foo6')).toEqualTypeOf() + expectTypeOf(c.var.foo6).toEqualTypeOf() + expectTypeOf(c.get('foo7')).toEqualTypeOf() + expectTypeOf(c.var.foo7).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + // @ts-expect-error foo5 is not typed + c.get('foo5') + // @ts-expect-error foo5 is not typed + c.var.foo5 + // @ts-expect-error foo6 is not typed + c.get('foo6') + // @ts-expect-error foo6 is not typed + c.var.foo6 + // @ts-expect-error foo7 is not typed + c.get('foo7') + // @ts-expect-error foo7 is not typed + c.var.foo7 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + expectTypeOf(c.get('foo5')).toEqualTypeOf() + expectTypeOf(c.var.foo5).toEqualTypeOf() + expectTypeOf(c.get('foo6')).toEqualTypeOf() + expectTypeOf(c.var.foo6).toEqualTypeOf() + expectTypeOf(c.get('foo7')).toEqualTypeOf() + expectTypeOf(c.var.foo7).toEqualTypeOf() + expectTypeOf(c.get('foo8')).toEqualTypeOf() + expectTypeOf(c.var.foo8).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + // @ts-expect-error foo5 is not typed + c.get('foo5') + // @ts-expect-error foo5 is not typed + c.var.foo5 + // @ts-expect-error foo6 is not typed + c.get('foo6') + // @ts-expect-error foo6 is not typed + c.var.foo6 + // @ts-expect-error foo7 is not typed + c.get('foo7') + // @ts-expect-error foo7 is not typed + c.var.foo7 + // @ts-expect-error foo8 is not typed + c.get('foo8') + // @ts-expect-error foo8 is not typed + c.var.foo8 + return c.json(0) + }) + + new Hono() + .get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + expectTypeOf(c.get('foo1')).toEqualTypeOf() + expectTypeOf(c.var.foo1).toEqualTypeOf() + expectTypeOf(c.get('foo2')).toEqualTypeOf() + expectTypeOf(c.var.foo2).toEqualTypeOf() + expectTypeOf(c.get('foo3')).toEqualTypeOf() + expectTypeOf(c.var.foo3).toEqualTypeOf() + expectTypeOf(c.get('foo4')).toEqualTypeOf() + expectTypeOf(c.var.foo4).toEqualTypeOf() + expectTypeOf(c.get('foo5')).toEqualTypeOf() + expectTypeOf(c.var.foo5).toEqualTypeOf() + expectTypeOf(c.get('foo6')).toEqualTypeOf() + expectTypeOf(c.var.foo6).toEqualTypeOf() + expectTypeOf(c.get('foo7')).toEqualTypeOf() + expectTypeOf(c.var.foo7).toEqualTypeOf() + expectTypeOf(c.get('foo8')).toEqualTypeOf() + expectTypeOf(c.var.foo8).toEqualTypeOf() + expectTypeOf(c.get('foo9')).toEqualTypeOf() + expectTypeOf(c.var.foo9).toEqualTypeOf() + return c.json(0) + }) + .get('/', (c) => { + expectTypeOf(c.get('init')).toEqualTypeOf() + expectTypeOf(c.var.init).toEqualTypeOf() + // @ts-expect-error foo1 is not typed + c.get('foo1') + // @ts-expect-error foo1 is not typed + c.var.foo1 + // @ts-expect-error foo2 is not typed + c.get('foo2') + // @ts-expect-error foo2 is not typed + c.var.foo2 + // @ts-expect-error foo3 is not typed + c.get('foo3') + // @ts-expect-error foo3 is not typed + c.var.foo3 + // @ts-expect-error foo4 is not typed + c.get('foo4') + // @ts-expect-error foo4 is not typed + c.var.foo4 + // @ts-expect-error foo5 is not typed + c.get('foo5') + // @ts-expect-error foo5 is not typed + c.var.foo5 + // @ts-expect-error foo6 is not typed + c.get('foo6') + // @ts-expect-error foo6 is not typed + c.var.foo6 + // @ts-expect-error foo7 is not typed + c.get('foo7') + // @ts-expect-error foo7 is not typed + c.var.foo7 + // @ts-expect-error foo8 is not typed + c.get('foo8') + // @ts-expect-error foo8 is not typed + c.var.foo8 + // @ts-expect-error foo9 is not typed + c.get('foo9') + // @ts-expect-error foo9 is not typed + c.var.foo9 + return c.json(0) + }) }) })