From db3387353f23e0914faf8169323c06e9d9658c20 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 12 Nov 2023 09:06:40 +0900 Subject: [PATCH] fix(context): implement `ContextVariableMap` for `c.var` (#1682) * fix(context): implement `ContextVariableMap` for `c.var` * denoify --- deno_dist/context.ts | 4 ++-- src/context.ts | 4 ++-- src/types.test.ts | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/deno_dist/context.ts b/deno_dist/context.ts index 3e3d87ec..9769e710 100644 --- a/deno_dist/context.ts +++ b/deno_dist/context.ts @@ -225,8 +225,8 @@ export class Context< } // c.var.propName is a read-only - get var(): Readonly { - return { ...this._var } + get var(): Readonly { + return { ...this._var } as never } newResponse: NewResponse = ( diff --git a/src/context.ts b/src/context.ts index 4ca04362..14c66b4e 100644 --- a/src/context.ts +++ b/src/context.ts @@ -225,8 +225,8 @@ export class Context< } // c.var.propName is a read-only - get var(): Readonly { - return { ...this._var } + get var(): Readonly { + return { ...this._var } as never } newResponse: NewResponse = ( diff --git a/src/types.test.ts b/src/types.test.ts index 396c65c5..378ac63e 100644 --- a/src/types.test.ts +++ b/src/types.test.ts @@ -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() + return c.json(0) + }) + new Hono().get((c) => { + expectTypeOf(c.var.payload).toEqualTypeOf() + return c.json(0) + }) + }) +})