Skip to content
Myr

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

3 files+158 added−20 removed

modified:deno_dist/types.ts+34 added-10 removed
81 unmodified lines
82 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>8788 // app.get(handler, handler)89 <90 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
2 unmodified lines
93 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>9899 // app.get(handler x 3)100 <101 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
5 unmodified lines
107 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>112113 // app.get(handler x 4)114 <115 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
7 unmodified lines
123 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>128129 // app.get(handler x 5)130 <131 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
15 unmodified lines
147 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 >156157 // app.get(...handlers[])158 <159 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
23 unmodified lines
183 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 >192193 // app.get(path, handler, handler)194 <195 P extends string,
5 unmodified lines
201 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 >210211 // app.get(path, handler x3)212 <213 P extends string,
13 unmodified lines
227 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 >236237 // app.get(path, handler x4)238 <239 P extends string,
17 unmodified lines
257 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 >266267 // app.get(path, handler x5)268 <269 P extends string,
21 unmodified lines
291 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 >300301 // app.get(path, ...handlers[])302 <P extends string, I extends Input = {}, R extends HandlerResponse<any> = any>(303 path: P,
315 unmodified lines
modified:src/types.test.ts+90 added
1/* eslint-disable @typescript-eslint/ban-types */2/* eslint-disable @typescript-eslint/no-unused-vars */3import { expectTypeOf } from 'vitest'4import type { Context } from './context'5import { createMiddleware } from './helper'6import { Hono } from './hono'7import { poweredBy } from './middleware/powered-by'8import type {9 Env,
9 unmodified lines
19 ToSchema,20 UndefinedIfHavingQuestion,21} from './types'22import type { Expect, Equal } from './utils/types'2324describe('Env', () => {25 test('Env', () => {26 type E = {27 Variables: {
617 unmodified lines
645 }646 type verify = Expect<Equal<Expected, Actual>>647 })648})649650/**651 * Other tests for `c.var` are written in `hono.test.ts`.652 * This tests are only for types.653 */654describe('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 () => {})660661 it('Should not throw type errors', () => {662 // app.get(handler...)663664 new Hono().get(mw1).get('/', (c) => {665 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()666 return c.json(0)667 })668669 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 })674675 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 })681682 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 })689690 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 })698699 // app.get('/', handler...)700701 new Hono().get('/', mw1).get('/', (c) => {702 expectTypeOf(c.var.foo1).toEqualTypeOf<string>()703 return c.json(0)704 })705706 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 })711712 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 })718719 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 })726727 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 lines
82 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>8788 // app.get(handler, handler)89 <90 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
2 unmodified lines
93 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>9899 // app.get(handler x 3)100 <101 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
5 unmodified lines
107 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>112113 // app.get(handler x 4)114 <115 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
7 unmodified lines
123 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>128129 // app.get(handler x 5)130 <131 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
15 unmodified lines
147 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 >156157 // app.get(...handlers[])158 <159 P extends string = ExtractKey<S> extends never ? BasePath : ExtractKey<S>,
23 unmodified lines
183 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 >192193 // app.get(path, handler, handler)194 <195 P extends string,
5 unmodified lines
201 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 >210211 // app.get(path, handler x3)212 <213 P extends string,
13 unmodified lines
227 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 >236237 // app.get(path, handler x4)238 <239 P extends string,
17 unmodified lines
257 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 >266267 // app.get(path, handler x5)268 <269 P extends string,
21 unmodified lines
291 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 >300301 // app.get(path, ...handlers[])302 <P extends string, I extends Input = {}, R extends HandlerResponse<any> = any>(303 path: P,
315 unmodified lines