Skip to content

Commit

Permalink
fix: degradation of generic type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
m-shaka committed Jul 14, 2024
1 parent 2d01359 commit 4d9f716
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 @@ -2240,3 +2240,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 4d9f716

Please sign in to comment.