0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-29 09:43:20 +01:00

fix(context): implement ContextVariableMap for c.var (#1682)

* fix(context): implement `ContextVariableMap` for `c.var`

* denoify
This commit is contained in:
Yusuke Wada 2023-11-12 09:06:40 +09:00 committed by GitHub
parent 47942ffc68
commit db3387353f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View File

@ -225,8 +225,8 @@ export class Context<
}
// c.var.propName is a read-only
get var(): Readonly<E['Variables']> {
return { ...this._var }
get var(): Readonly<E['Variables'] & ContextVariableMap> {
return { ...this._var } as never
}
newResponse: NewResponse = (

View File

@ -225,8 +225,8 @@ export class Context<
}
// c.var.propName is a read-only
get var(): Readonly<E['Variables']> {
return { ...this._var }
get var(): Readonly<E['Variables'] & ContextVariableMap> {
return { ...this._var } as never
}
newResponse: NewResponse = (

View File

@ -734,3 +734,26 @@ describe('c.var with chaining - test only types', () => {
})
})
})
/**
*
* Declaring a ContextVariableMap for testing.
*/
declare module './context' {
interface ContextVariableMap {
payload: string
}
}
describe('c.var with ContextVariableMap - test only types', () => {
it('Should no throw a type error', () => {
new Hono().get((c) => {
expectTypeOf(c.get('payload')).toEqualTypeOf<string>()
return c.json(0)
})
new Hono().get((c) => {
expectTypeOf(c.var.payload).toEqualTypeOf<string>()
return c.json(0)
})
})
})