Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): correct c.get() inference #1995

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { Context } from './context.ts'
import type { Hono } from './hono.ts'
import type { IntersectNonAnyTypes, UnionToIntersection } from './utils/types.ts'
import type { IfAnyThenEmptyObject, UnionToIntersection } from './utils/types.ts'

////////////////////////////////////////
////// //////
Expand Down Expand Up @@ -1561,6 +1561,11 @@ export type ExtractSchema<T> = UnionToIntersection<
T extends Hono<infer _, infer S, any> ? S : never
>

type EnvOrEmpty<T> = T extends Env ? (Env extends T ? {} : T) : T
type IntersectNonAnyTypes<T extends any[]> = T extends [infer Head, ...infer Rest]
? IfAnyThenEmptyObject<EnvOrEmpty<Head>> & IntersectNonAnyTypes<Rest>
: {}

////////////////////////////////////////
////// //////
////// FetchEvent //////
Expand Down
4 changes: 0 additions & 4 deletions deno_dist/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export type RemoveBlankRecord<T> = T extends Record<infer K, unknown>

export type IfAnyThenEmptyObject<T> = 0 extends 1 & T ? {} : T

export type IntersectNonAnyTypes<T extends any[]> = T extends [infer Head, ...infer Rest]
? IfAnyThenEmptyObject<Head> & IntersectNonAnyTypes<Rest>
: {}

export type JSONPrimitive = string | boolean | number | null | undefined
export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]
export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }
Expand Down
55 changes: 55 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,24 +839,34 @@ describe('c.var with chaining - test only types', () => {
// app.get('/', handler...)

new Hono().get('/', mw1).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
return c.json(0)
})

new Hono().get('/', mw1, mw2, mw3, mw4).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -865,6 +875,11 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -874,6 +889,12 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -884,6 +905,13 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -895,6 +923,14 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -907,6 +943,15 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand All @@ -920,6 +965,16 @@ describe('c.var with chaining - test only types', () => {
})

new Hono().get('/', mw1, mw2, mw3, mw4, mw5, mw6, mw7, mw8, mw9, mw10).get('/', (c) => {
expectTypeOf(c.get('foo1')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo2')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo3')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo4')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo5')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo6')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo7')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo8')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo9')).toEqualTypeOf<string>()
expectTypeOf(c.get('foo10')).toEqualTypeOf<string>()
expectTypeOf(c.var.foo1).toEqualTypeOf<string>()
expectTypeOf(c.var.foo2).toEqualTypeOf<string>()
expectTypeOf(c.var.foo3).toEqualTypeOf<string>()
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable @typescript-eslint/ban-types */
import type { Context } from './context'
import type { Hono } from './hono'
import type { IntersectNonAnyTypes, UnionToIntersection } from './utils/types'
import type { IfAnyThenEmptyObject, UnionToIntersection } from './utils/types'

////////////////////////////////////////
////// //////
Expand Down Expand Up @@ -1561,6 +1561,11 @@ export type ExtractSchema<T> = UnionToIntersection<
T extends Hono<infer _, infer S, any> ? S : never
>

type EnvOrEmpty<T> = T extends Env ? (Env extends T ? {} : T) : T
type IntersectNonAnyTypes<T extends any[]> = T extends [infer Head, ...infer Rest]
? IfAnyThenEmptyObject<EnvOrEmpty<Head>> & IntersectNonAnyTypes<Rest>
: {}

////////////////////////////////////////
////// //////
////// FetchEvent //////
Expand Down
4 changes: 0 additions & 4 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export type RemoveBlankRecord<T> = T extends Record<infer K, unknown>

export type IfAnyThenEmptyObject<T> = 0 extends 1 & T ? {} : T

export type IntersectNonAnyTypes<T extends any[]> = T extends [infer Head, ...infer Rest]
? IfAnyThenEmptyObject<Head> & IntersectNonAnyTypes<Rest>
: {}

export type JSONPrimitive = string | boolean | number | null | undefined
export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]
export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }
Expand Down