Skip to content
Myr

fix(types): use `ContextVariableMap` in `Context<any>` (#3134)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(types): use `ContextVariableMap` in `Context<any>` * use `any`

authored by Yusuke Wada · committed by GitHub · parent 1afecac · Browse files · 2 files+25 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+25 added−4 removed

  • tests:+9 added−1 removed
  • other:+16 added−3 removed

2 files+25 added−4 removed

modified:src/context.ts+16 added-3 removed
517 unmodified lines
518 * c.set('message', 'Hono is cool!!')519 * await next()520 * })521 * ```522```522 */524 set: Set<E> = (key: unknown, value: unknown) => {523 set: Set<524 IsAny<E> extends true525 ? {526 // eslint-disable-next-line @typescript-eslint/no-explicit-any527 Variables: ContextVariableMap & Record<string, any>528 }529 : E530 > = (key: string, value: unknown) => {531 this.#var ??= new Map()532 this.#var.set(key, value)533 }534
9 unmodified lines
544 * return c.text(`The message is "${message}"`)545 * })546 * ```547 */542 get: Get<E> = (key: unknown) => {548 get: Get<549 IsAny<E> extends true550 ? {551 // eslint-disable-next-line @typescript-eslint/no-explicit-any552 Variables: ContextVariableMap & Record<string, any>553 }554 : E555 > = (key: string) => {556 return this.#var ? this.#var.get(key) : undefined557 }558559 /**
249 unmodified lines
modified:src/types.test.ts+9 added-1 removed
1/* eslint-disable @typescript-eslint/ban-types */2/* eslint-disable @typescript-eslint/no-unused-vars */3import { expectTypeOf } from 'vitest'4import type { Context } from './context'4import { Context } from './context'5import { createMiddleware } from './helper/factory'6import { Hono } from './hono'7import { poweredBy } from './middleware/powered-by'8import type {
1738 unmodified lines
1747 expectTypeOf(c.get('payload')).toEqualTypeOf<number>()1748 return c.json(0)1749 })1750 })17511752 it('Should use ContextVariableMap when c is Context<any>', () => {1753 const c = new Context(new Request('http://localhost'))1754 expectTypeOf(c.get('payload')).toEqualTypeOf<string>()1755 expectTypeOf(c.var.payload).toEqualTypeOf<string>()1756 // @ts-expect-error the value of payload should be string1757 expectTypeOf(c.set('payload', 123))1758 })1759})17601761/**1762 * It's challenge to test all cases. This is a minimal pattern.
488 unmodified lines