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