From d284ca76b5883a55fc2c0365c17e472e3d2ea4e7 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Thu, 27 Nov 2025 19:24:53 +0900 Subject: [PATCH] feat(validator): infer the context type in validation func --- src/validator/validator.test.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/validator/validator.test.ts b/src/validator/validator.test.ts index 493032e4fd..c56e48cabe 100644 --- a/src/validator/validator.test.ts +++ b/src/validator/validator.test.ts @@ -1368,3 +1368,34 @@ describe('Raw Request cloning after validation', () => { expect(result.payload).toMatchObject(testData) }) }) + +describe('Type inference in a validation function', () => { + type ExtractEnvFromContext = C extends Context ? E : never + type ExtractPathFromContext = C extends Context ? P : never + + const app = new Hono<{ + Variables: { + foo: string + } + }>() + + it('Should not throw type errors for the context', () => { + app.post( + '/user/:name', + validator('json', (_data, c) => { + type P = ExtractPathFromContext + type verify = Expect> + type E = ExtractEnvFromContext + type verify2 = Expect> + return {} + }), + (c) => { + type P = ExtractPathFromContext + type verify = Expect> + type E = ExtractEnvFromContext + type verify2 = Expect> + return c.json({}) + } + ) + }) +})