diff --git a/src/helper/adapter/index.test.ts b/src/helper/adapter/index.test.ts index 8c9cb35bf..de46496bd 100644 --- a/src/helper/adapter/index.test.ts +++ b/src/helper/adapter/index.test.ts @@ -1,4 +1,5 @@ -import { getRuntimeKey } from '.' +import { Hono } from '../../hono' +import { env, getRuntimeKey } from '.' describe('getRuntimeKey', () => { it('Should return the current runtime key', () => { @@ -7,3 +8,22 @@ describe('getRuntimeKey', () => { expect(getRuntimeKey()).toBe('node') }) }) + +describe('env', () => { + describe('Types', () => { + type Env = { + Bindings: { + MY_VAR: string + } + } + const app = new Hono() + + 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, + }) + }) + }) +}) diff --git a/src/helper/adapter/index.ts b/src/helper/adapter/index.ts index 01e73538d..847ad0206 100644 --- a/src/helper/adapter/index.ts +++ b/src/helper/adapter/index.ts @@ -7,7 +7,14 @@ import type { Context } from '../../context' export type Runtime = 'node' | 'deno' | 'bun' | 'workerd' | 'fastly' | 'edge-light' | 'other' -export const env = , C extends Context = Context<{}>>( +export const env = < + T extends Record, + C extends Context = Context< + {} & { + Bindings: T + } + > +>( c: C, runtime?: Runtime ): T & C['env'] => {