From 0ac27fc817eb15c64f57981a93353023d2b96c34 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 29 Aug 2023 23:07:27 +0900 Subject: [PATCH] fix(types): infer a response type for async handler (#1385) * fix(types): infer a response type for async handler * denoify --- deno_dist/types.ts | 8 +++++++- src/types.test.ts | 23 +++++++++++++++++++++++ src/types.ts | 8 +++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/deno_dist/types.ts b/deno_dist/types.ts index 3586c6b8..de96806a 100644 --- a/deno_dist/types.ts +++ b/deno_dist/types.ts @@ -387,7 +387,13 @@ export type TypedResponse = { format: 'json' // Currently, support only `json` with `c.jsonT()` } -type ExtractResponseData = T extends TypedResponse ? U : never +type ExtractResponseData = T extends Promise + ? T2 extends TypedResponse + ? U + : never + : T extends TypedResponse + ? U + : never type MergeTypedResponseData = UnionToIntersection> diff --git a/src/types.test.ts b/src/types.test.ts index 526cca04..564f0a8f 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -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 + type Expected = { + '/': { + $get: { + input: {} + output: { + ok: boolean + } + } + } + } + type verify = Expect> + }) +}) diff --git a/src/types.ts b/src/types.ts index 59786de7..d821c3d6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -387,7 +387,13 @@ export type TypedResponse = { format: 'json' // Currently, support only `json` with `c.jsonT()` } -type ExtractResponseData = T extends TypedResponse ? U : never +type ExtractResponseData = T extends Promise + ? T2 extends TypedResponse + ? U + : never + : T extends TypedResponse + ? U + : never type MergeTypedResponseData = UnionToIntersection>