mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
fix(types): infer a response type for async handler (#1385)
* fix(types): infer a response type for async handler * denoify
This commit is contained in:
parent
5d24a87bea
commit
0ac27fc817
@ -387,7 +387,13 @@ export type TypedResponse<T = unknown> = {
|
||||
format: 'json' // Currently, support only `json` with `c.jsonT()`
|
||||
}
|
||||
|
||||
type ExtractResponseData<T> = T extends TypedResponse<infer U> ? U : never
|
||||
type ExtractResponseData<T> = T extends Promise<infer T2>
|
||||
? T2 extends TypedResponse<infer U>
|
||||
? U
|
||||
: never
|
||||
: T extends TypedResponse<infer U>
|
||||
? U
|
||||
: never
|
||||
|
||||
type MergeTypedResponseData<T> = UnionToIntersection<ExtractResponseData<T>>
|
||||
|
||||
|
@ -595,3 +595,26 @@ describe('Different types using jsonT()', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('jsonT() in an async handler', () => {
|
||||
const app = new Hono()
|
||||
test('Three different types', () => {
|
||||
const route = app.get(async (c) => {
|
||||
return c.jsonT({
|
||||
ok: true,
|
||||
})
|
||||
})
|
||||
type Actual = ExtractSchema<typeof route>
|
||||
type Expected = {
|
||||
'/': {
|
||||
$get: {
|
||||
input: {}
|
||||
output: {
|
||||
ok: boolean
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
type verify = Expect<Equal<Expected, Actual>>
|
||||
})
|
||||
})
|
||||
|
@ -387,7 +387,13 @@ export type TypedResponse<T = unknown> = {
|
||||
format: 'json' // Currently, support only `json` with `c.jsonT()`
|
||||
}
|
||||
|
||||
type ExtractResponseData<T> = T extends TypedResponse<infer U> ? U : never
|
||||
type ExtractResponseData<T> = T extends Promise<infer T2>
|
||||
? T2 extends TypedResponse<infer U>
|
||||
? U
|
||||
: never
|
||||
: T extends TypedResponse<infer U>
|
||||
? U
|
||||
: never
|
||||
|
||||
type MergeTypedResponseData<T> = UnionToIntersection<ExtractResponseData<T>>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user