Skip to content

Commit

Permalink
fix(type): degradation of generic type handling (#3138)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-shaka authored Jul 22, 2024
1 parent 4d33e1c commit 8dc9e48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type JSONRespondReturn<
? JSONValue extends SimplifyDeepArray<T>
? never
: JSONParsed<T>
: JSONParsed<T>,
: never,
U,
'json'
>
Expand Down
21 changes: 21 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,3 +2248,24 @@ describe('Returning type from `app.use(path, mw)`', () => {
type verify = Expect<Equal<Expected, Actual>>
})
})
describe('generic typed variables', () => {
type Variables = {
ok: <TData>(data: TData) => TypedResponse<{ data: TData }>
}
const app = new Hono<{ Variables: Variables }>()

it('Should set and get variables with correct types', async () => {
const route = app
.use('*', async (c, next) => {
c.set('ok', (data) => c.json({ data }))
await next()
})
.get('/', (c) => {
const ok = c.get('ok')
return ok('Hello')
})
type Actual = ExtractSchema<typeof route>['/']['$get']['output']
type Expected = { data: string }
expectTypeOf<Actual>().toEqualTypeOf<Expected>()
})
})
4 changes: 3 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export type IfAnyThenEmptyObject<T> = 0 extends 1 & T ? {} : T

export type JSONPrimitive = string | boolean | number | null
export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]
export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject | object }
export type JSONObject = {
[key: string]: JSONPrimitive | JSONArray | JSONObject | object | InvalidJSONValue
}
export type InvalidJSONValue = undefined | symbol | ((...args: unknown[]) => unknown)

type InvalidToNull<T> = T extends InvalidJSONValue ? null : T
Expand Down

0 comments on commit 8dc9e48

Please sign in to comment.