Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): env variables override ContextVariableMap #2987

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export type Layout<T = Record<string, any>> = (props: T) => any
* @template E - Environment type.
*/
interface Get<E extends Env> {
<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]
<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
}

/**
Expand All @@ -79,8 +79,8 @@ interface Get<E extends Env> {
* @template E - Environment type.
*/
interface Set<E extends Env> {
<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void
<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1638,8 +1638,8 @@ declare module './context' {
}
}

describe('c.var with ContextVariableMap - test only types', () => {
it('Should no throw a type error', () => {
describe('ContextVariableMap type tests', () => {
it('Should not throw type errors with c.var', () => {
new Hono().get((c) => {
expectTypeOf(c.get('payload')).toEqualTypeOf<string>()
return c.json(0)
Expand All @@ -1649,6 +1649,22 @@ describe('c.var with ContextVariableMap - test only types', () => {
return c.json(0)
})
})

it('Should override ContextVariableMap with env variables', () => {
const middleware = createMiddleware<{
Variables: {
payload: number
}
}>(async (c, next) => {
c.set('payload', 123)
await next()
})

new Hono().get(middleware, (c) => {
expectTypeOf(c.get('payload')).toEqualTypeOf<number>()
return c.json(0)
})
})
})

/**
Expand Down