From 8dc9e4899c8ff71b68f3467a2bfebb6aab862c42 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Mon, 22 Jul 2024 15:44:46 +0900 Subject: [PATCH] fix(type): degradation of generic type handling (#3138) --- src/context.ts | 2 +- src/types.test.ts | 21 +++++++++++++++++++++ src/utils/types.ts | 4 +++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/context.ts b/src/context.ts index 8d0aa89e..365161fc 100644 --- a/src/context.ts +++ b/src/context.ts @@ -180,7 +180,7 @@ type JSONRespondReturn< ? JSONValue extends SimplifyDeepArray ? never : JSONParsed - : JSONParsed, + : never, U, 'json' > diff --git a/src/types.test.ts b/src/types.test.ts index b1d1b55d..bdb1ef27 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -2248,3 +2248,24 @@ describe('Returning type from `app.use(path, mw)`', () => { type verify = Expect> }) }) +describe('generic typed variables', () => { + type Variables = { + ok: (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['/']['$get']['output'] + type Expected = { data: string } + expectTypeOf().toEqualTypeOf() + }) +}) diff --git a/src/utils/types.ts b/src/utils/types.ts index 3fd17a94..65e69ce8 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -26,7 +26,9 @@ export type IfAnyThenEmptyObject = 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 extends InvalidJSONValue ? null : T