From 4d9f7164c8412b747ce7739ccc67fae11ef1e8c2 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Mon, 15 Jul 2024 07:31:57 +0900 Subject: [PATCH] fix: degradation of generic type handling --- 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 a24fa26dd..18b8d324b 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 c94d1b65d..fac770853 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -2240,3 +2240,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 8beb970a6..2493b92da 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