fix(types): infer env types with chaining handlers (#1668)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(types): infer env types with chaining handlers * denoify
authored by Yusuke Wada · committed by GitHub · parent 88e89a4 · Browse files · 3 files·+158 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+158 added−20 removed
- tests:+90 added−0 removed
- other:+68 added−20 removed
Provenance
- Commit
c6fbf84- Tree
1634987- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
88e89a4
3 files changed+158 added−20 removed
modified:deno_dist/types.ts+34 added-10 removed
81 unmodified lines82 R extends HandlerResponse<any> = any,83 E2 extends Env = E84 >(85 handler: H<E2, P, I, R>86- ): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>86+ ): Hono<E & E2, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>87 88 // app.get(handler, handler)89 <90 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,2 unmodified lines93 E2 extends Env = E,94 E3 extends Env = IntersectNonAnyTypes<[E, E2]>95 >(96 ...handlers: [H<E2, P, I, R>, H<E3, P, I, R>]97- ): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>97+ ): Hono<E & E2 & E3, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>98 99 // app.get(handler x 3)100 <101 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,5 unmodified lines107 E3 extends Env = E,108 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>109 >(110 ...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>]111- ): Hono<E, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>111+ ): Hono<E & E2 & E3 & E4, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>112 113 // app.get(handler x 4)114 <115 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,7 unmodified lines123 E4 extends Env = E,124 E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>125 >(126 ...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>, H<E5, P, I3, R>]127- ): Hono<E, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>127+ ): Hono<E & E2 & E3 & E4 & E5, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>128 129 // app.get(handler x 5)130 <131 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,15 unmodified lines147 H<E4, P, I3, R>,148 H<E5, P, I3, R>,149 H<E6, P, I3, R>150 ]151- ): Hono<E, S & ToSchema<M, P, I5['in'], MergeTypedResponseData<R>>, BasePath>151+ ): Hono<152+ E & E2 & E3 & E4 & E5 & E6,153+ S & ToSchema<M, P, I5['in'], MergeTypedResponseData<R>>,154+ BasePath155+ >156 157 // app.get(...handlers[])158 <159 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,23 unmodified lines183 E2 extends Env = E184 >(185 path: P,186 handler: H<E2, MergePath<BasePath, P2>, I, R>183- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>187+ ): Hono<188+ E & E2,189+ S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,190+ BasePath191+ >192 193 // app.get(path, handler, handler)194 <195 P extends string,5 unmodified lines201 E3 extends Env = IntersectNonAnyTypes<[E, E2]>202 >(203 path: P,204 ...handlers: [H<E2, MergePath<BasePath, P2>, I, R>, H<E3, MergePath<BasePath, P3>, I, R>]197- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>205+ ): Hono<206+ E & E2 & E3,207+ S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,208+ BasePath209+ >210 211 // app.get(path, handler x3)212 <213 P extends string,13 unmodified lines227 H<E2, MergePath<BasePath, P2>, I, R>,228 H<E3, MergePath<BasePath, P3>, I2, R>,229 H<E4, MergePath<BasePath, P4>, I3, R>230 ]219- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>, BasePath>231+ ): Hono<232+ E & E2 & E3 & E4,233+ S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>,234+ BasePath235+ >236 237 // app.get(path, handler x4)238 <239 P extends string,17 unmodified lines257 H<E3, MergePath<BasePath, P3>, I2, R>,258 H<E4, MergePath<BasePath, P4>, I3, R>,259 H<E5, MergePath<BasePath, P5>, I4, R>260 ]245- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>, BasePath>261+ ): Hono<262+ E & E2 & E3 & E4 & E5,263+ S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>,264+ BasePath265+ >266 267 // app.get(path, handler x5)268 <269 P extends string,21 unmodified lines291 H<E4, MergePath<BasePath, P4>, I3, R>,292 H<E5, MergePath<BasePath, P5>, I4, R>,293 H<E6, MergePath<BasePath, P6>, I5, R>294 ]275- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>, BasePath>295+ ): Hono<296+ E & E2 & E3 & E4 & E5 & E6,297+ S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>,298+ BasePath299+ >300 301 // app.get(path, ...handlers[])302 <P extends string, I extends Input = {}, R extends HandlerResponse<any> = any>(303 path: P,315 unmodified linesmodified:src/types.test.ts+90 added
1 /* eslint-disable @typescript-eslint/ban-types */2 /* eslint-disable @typescript-eslint/no-unused-vars */3 import { expectTypeOf } from 'vitest'4 import type { Context } from './context'5+import { createMiddleware } from './helper'6 import { Hono } from './hono'7 import { poweredBy } from './middleware/powered-by'8 import type {9 Env,9 unmodified lines19 ToSchema,20 UndefinedIfHavingQuestion,21 } from './types'22 import type { Expect, Equal } from './utils/types'23+24 describe('Env', () => {25 test('Env', () => {26 type E = {27 Variables: {617 unmodified lines645 }646 type verify = Expect<Equal<Expected, Actual>>647 })648 })649+650+/**651+ * Other tests for `c.var` are written in `hono.test.ts`.652+ * This tests are only for types.653+ */654+describe('c.var with chaining - test only types', () => {655+ const mw1 = createMiddleware<{ Variables: { foo1: string } }>(async () => {})656+ const mw2 = createMiddleware<{ Variables: { foo2: string } }>(async () => {})657+ const mw3 = createMiddleware<{ Variables: { foo3: string } }>(async () => {})658+ const mw4 = createMiddleware<{ Variables: { foo4: string } }>(async () => {})659+ const mw5 = createMiddleware<{ Variables: { foo5: string } }>(async () => {})660+661+ it('Should not throw type errors', () => {662+ // app.get(handler...)663+664+ new Hono().get(mw1).get('/', (c) => {665+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()666+ return c.json(0)667+ })668+669+ new Hono().get(mw1, mw2).get('/', (c) => {670+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()671+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()672+ return c.json(0)673+ })674+675+ new Hono().get(mw1, mw2, mw3).get('/', (c) => {676+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()677+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()678+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()679+ return c.json(0)680+ })681+682+ new Hono().get(mw1, mw2, mw3, mw4).get('/', (c) => {683+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()684+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()685+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()686+ expectTypeOf(c.var.foo4).toEqualTypeOf<string>()687+ return c.json(0)688+ })689+690+ new Hono().get(mw1, mw2, mw3, mw4, mw5).get('/', (c) => {691+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()692+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()693+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()694+ expectTypeOf(c.var.foo4).toEqualTypeOf<string>()695+ expectTypeOf(c.var.foo5).toEqualTypeOf<string>()696+ return c.json(0)697+ })698+699+ // app.get('/', handler...)700+701+ new Hono().get('/', mw1).get('/', (c) => {702+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()703+ return c.json(0)704+ })705+706+ new Hono().get('/', mw1, mw2).get('/', (c) => {707+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()708+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()709+ return c.json(0)710+ })711+712+ new Hono().get('/', mw1, mw2, mw3).get('/', (c) => {713+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()714+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()715+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()716+ return c.json(0)717+ })718+719+ new Hono().get('/', mw1, mw2, mw3, mw4).get('/', (c) => {720+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()721+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()722+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()723+ expectTypeOf(c.var.foo4).toEqualTypeOf<string>()724+ return c.json(0)725+ })726+727+ new Hono().get('/', mw1, mw2, mw3, mw4, mw5).get('/', (c) => {728+ expectTypeOf(c.var.foo1).toEqualTypeOf<string>()729+ expectTypeOf(c.var.foo2).toEqualTypeOf<string>()730+ expectTypeOf(c.var.foo3).toEqualTypeOf<string>()731+ expectTypeOf(c.var.foo4).toEqualTypeOf<string>()732+ expectTypeOf(c.var.foo5).toEqualTypeOf<string>()733+ return c.json(0)734+ })735+ })736+})modified:src/types.ts+34 added-10 removed
81 unmodified lines82 R extends HandlerResponse<any> = any,83 E2 extends Env = E84 >(85 handler: H<E2, P, I, R>86- ): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>86+ ): Hono<E & E2, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>87 88 // app.get(handler, handler)89 <90 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,2 unmodified lines93 E2 extends Env = E,94 E3 extends Env = IntersectNonAnyTypes<[E, E2]>95 >(96 ...handlers: [H<E2, P, I, R>, H<E3, P, I, R>]97- ): Hono<E, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>97+ ): Hono<E & E2 & E3, S & ToSchema<M, P, I['in'], MergeTypedResponseData<R>>, BasePath>98 99 // app.get(handler x 3)100 <101 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,5 unmodified lines107 E3 extends Env = E,108 E4 extends Env = IntersectNonAnyTypes<[E, E2, E3]>109 >(110 ...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>]111- ): Hono<E, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>111+ ): Hono<E & E2 & E3 & E4, S & ToSchema<M, P, I3['in'], MergeTypedResponseData<R>>, BasePath>112 113 // app.get(handler x 4)114 <115 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,7 unmodified lines123 E4 extends Env = E,124 E5 extends Env = IntersectNonAnyTypes<[E, E2, E3, E4]>125 >(126 ...handlers: [H<E2, P, I, R>, H<E3, P, I2, R>, H<E4, P, I3, R>, H<E5, P, I3, R>]127- ): Hono<E, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>127+ ): Hono<E & E2 & E3 & E4 & E5, S & ToSchema<M, P, I4['in'], MergeTypedResponseData<R>>, BasePath>128 129 // app.get(handler x 5)130 <131 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,15 unmodified lines147 H<E4, P, I3, R>,148 H<E5, P, I3, R>,149 H<E6, P, I3, R>150 ]151- ): Hono<E, S & ToSchema<M, P, I5['in'], MergeTypedResponseData<R>>, BasePath>151+ ): Hono<152+ E & E2 & E3 & E4 & E5 & E6,153+ S & ToSchema<M, P, I5['in'], MergeTypedResponseData<R>>,154+ BasePath155+ >156 157 // app.get(...handlers[])158 <159 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,23 unmodified lines183 E2 extends Env = E184 >(185 path: P,186 handler: H<E2, MergePath<BasePath, P2>, I, R>183- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>187+ ): Hono<188+ E & E2,189+ S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,190+ BasePath191+ >192 193 // app.get(path, handler, handler)194 <195 P extends string,5 unmodified lines201 E3 extends Env = IntersectNonAnyTypes<[E, E2]>202 >(203 path: P,204 ...handlers: [H<E2, MergePath<BasePath, P2>, I, R>, H<E3, MergePath<BasePath, P3>, I, R>]197- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>, BasePath>205+ ): Hono<206+ E & E2 & E3,207+ S & ToSchema<M, MergePath<BasePath, P>, I['in'], MergeTypedResponseData<R>>,208+ BasePath209+ >210 211 // app.get(path, handler x3)212 <213 P extends string,13 unmodified lines227 H<E2, MergePath<BasePath, P2>, I, R>,228 H<E3, MergePath<BasePath, P3>, I2, R>,229 H<E4, MergePath<BasePath, P4>, I3, R>230 ]219- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>, BasePath>231+ ): Hono<232+ E & E2 & E3 & E4,233+ S & ToSchema<M, MergePath<BasePath, P>, I3['in'], MergeTypedResponseData<R>>,234+ BasePath235+ >236 237 // app.get(path, handler x4)238 <239 P extends string,17 unmodified lines257 H<E3, MergePath<BasePath, P3>, I2, R>,258 H<E4, MergePath<BasePath, P4>, I3, R>,259 H<E5, MergePath<BasePath, P5>, I4, R>260 ]245- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>, BasePath>261+ ): Hono<262+ E & E2 & E3 & E4 & E5,263+ S & ToSchema<M, MergePath<BasePath, P>, I4['in'], MergeTypedResponseData<R>>,264+ BasePath265+ >266 267 // app.get(path, handler x5)268 <269 P extends string,21 unmodified lines291 H<E4, MergePath<BasePath, P4>, I3, R>,292 H<E5, MergePath<BasePath, P5>, I4, R>,293 H<E6, MergePath<BasePath, P6>, I5, R>294 ]275- ): Hono<E, S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>, BasePath>295+ ): Hono<296+ E & E2 & E3 & E4 & E5 & E6,297+ S & ToSchema<M, MergePath<BasePath, P>, I5['in'], MergeTypedResponseData<R>>,298+ BasePath299+ >300 301 // app.get(path, ...handlers[])302 <P extends string, I extends Input = {}, R extends HandlerResponse<any> = any>(303 path: P,315 unmodified lines