0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 19:26:56 +01:00

fix(type): degradation of generic type handling (#3138)

This commit is contained in:
m-shaka 2024-07-22 15:44:46 +09:00 committed by GitHub
parent 4d33e1c0d4
commit 8dc9e4899c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 2 deletions

View File

@ -180,7 +180,7 @@ type JSONRespondReturn<
? JSONValue extends SimplifyDeepArray<T>
? never
: JSONParsed<T>
: JSONParsed<T>,
: never,
U,
'json'
>

View File

@ -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>()
})
})

View File

@ -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