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:
parent
4d33e1c0d4
commit
8dc9e4899c
@ -180,7 +180,7 @@ type JSONRespondReturn<
|
||||
? JSONValue extends SimplifyDeepArray<T>
|
||||
? never
|
||||
: JSONParsed<T>
|
||||
: JSONParsed<T>,
|
||||
: never,
|
||||
U,
|
||||
'json'
|
||||
>
|
||||
|
@ -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>()
|
||||
})
|
||||
})
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user