Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,34 @@
expect(result.payload).toMatchObject(testData)
})
})

describe('Type inference in a validation function', () => {
type ExtractEnvFromContext<C extends Context> = C extends Context<infer E> ? E : never
type ExtractPathFromContext<C extends Context> = C extends Context<infer E, infer P> ? 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<typeof c>
type verify = Expect<Equal<'/user/:name', P>>

Check failure on line 1387 in src/validator/validator.test.ts

View workflow job for this annotation

GitHub Actions / Main

Type 'false' does not satisfy the constraint 'true'.
type E = ExtractEnvFromContext<typeof c>
type verify2 = Expect<Equal<{ Variables: { foo: string } }, E>>

Check failure on line 1389 in src/validator/validator.test.ts

View workflow job for this annotation

GitHub Actions / Main

Type 'false' does not satisfy the constraint 'true'.
return {}
}),
(c) => {
type P = ExtractPathFromContext<typeof c>
type verify = Expect<Equal<'/user/:name', P>>
type E = ExtractEnvFromContext<typeof c>
type verify2 = Expect<Equal<{ Variables: { foo: string } }, E>>
return c.json({})
}
)
})
})
Loading