0
0
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:
Yusuke Wada 2023-08-29 23:07:27 +09:00 committed by GitHub
parent 5d24a87bea
commit 0ac27fc817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

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

View File

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

View File

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