Skip to content

Commit

Permalink
fix(helper/adapter): env should set c type correctly (#3856)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jan 26, 2025
1 parent d75e9ec commit f5e9f3a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/helper/adapter/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getRuntimeKey } from '.'
import { Hono } from '../../hono'
import { env, getRuntimeKey } from '.'

describe('getRuntimeKey', () => {
it('Should return the current runtime key', () => {
Expand All @@ -7,3 +8,22 @@ describe('getRuntimeKey', () => {
expect(getRuntimeKey()).toBe('node')
})
})

describe('env', () => {
describe('Types', () => {
type Env = {
Bindings: {
MY_VAR: string
}
}
const app = new Hono<Env>()

it('Should set the type of the Context correctly and not throw a type error')
app.get('/var', (c) => {
const { MY_VAR } = env<{ MY_VAR: string }>(c)
return c.json({
var: MY_VAR,
})
})
})
})
9 changes: 8 additions & 1 deletion src/helper/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type { Context } from '../../context'

export type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'other'

export const env = <T extends Record<string, unknown>, C extends Context = Context<{}>>(
export const env = <
T extends Record<string, unknown>,
C extends Context = Context<
{} & {
Bindings: T
}
>
>(
c: C,
runtime?: Runtime
): T & C['env'] => {
Expand Down

0 comments on commit f5e9f3a

Please sign in to comment.