Repository: honojs/hono
File: src/context.ts (TypeScript)

Code before:
import type { ContextVariableMap, NotFoundHandler } from './hono'
import type { CookieOptions } from './utils/cookie'
import { serialize } from './utils/cookie'
import type { StatusCode } from './utils/http-status'
import { isAbsoluteURL } from './utils/url'

type Headers = Record<string, string>
export type Data = string | ArrayBuffer | ReadableStream
type Env = Record<string, any>

export interface Context<RequestParamKeyType extends string = string, E = Env> {
  req: Request<RequestParamKeyType>
  env: E
  event: FetchEvent
  executionCtx: ExecutionContext
  finalized: boolean

  get res(): Response
  set res(_res: Response)
  header: (name: string, value: string) => void
  status: (status: StatusCode) => void
  set: (key: string, value: any) => void
  get: {
    <Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
    <T = any>(key: string): T
  }
  pretty: (prettyJSON: boolean, space?: number) => void
  newResponse: (data: Data | null, status: StatusCode, headers: Headers) => Response
  body: (data: Data | null, status?: StatusCode, headers?: Headers) => Response
  text: (text: string, status?: StatusCode, headers?: Headers) => Response
  json: <T>(object: T, status?: StatusCode, headers?: Headers) => Response
  html: (html: string, status?: StatusCode, headers?: Headers) => Response
  redirect: (location: string, status?: StatusCode) => Response
  cookie: (name: string, value: string, options?: CookieOptions) => void
  notFound: () => Response | Promise<Response>
}

export class HonoContext<RequestParamKeyType extends string = string, E = Env>
  implements Context<RequestParamKeyType, E>
{
  req: Request<RequestParamKeyType>
  env: E
  finalized: boolean

  _status: StatusCode = 200
  private _executionCtx: FetchEvent | ExecutionContext | undefined
  private _pretty: boolean = false

Review comment (suggestion):
Set should probably also attempt to enforce the type of `value` if it's specified on `ContextVariableMap` for extra type safety.

Code after:
import type { ContextVariableMap, NotFoundHandler } from './hono'
import type { CookieOptions } from './utils/cookie'
import { serialize } from './utils/cookie'
import type { StatusCode } from './utils/http-status'
import { isAbsoluteURL } from './utils/url'

type Headers = Record<string, string>
export type Data = string | ArrayBuffer | ReadableStream
type Env = Record<string, any>

export interface Context<RequestParamKeyType extends string = string, E = Env> {
  req: Request<RequestParamKeyType>
  env: E
  event: FetchEvent
  executionCtx: ExecutionContext
  finalized: boolean

  get res(): Response
  set res(_res: Response)
  header: (name: string, value: string) => void
  status: (status: StatusCode) => void
  set: {
    <Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
    (key: string, value: any): void
  }
  get: {
    <Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
    <T = any>(key: string): T
  }
  pretty: (prettyJSON: boolean, space?: number) => void
  newResponse: (data: Data | null, status: StatusCode, headers: Headers) => Response
  body: (data: Data | null, status?: StatusCode, headers?: Headers) => Response
  text: (text: string, status?: StatusCode, headers?: Headers) => Response
  json: <T>(object: T, status?: StatusCode, headers?: Headers) => Response
  html: (html: string, status?: StatusCode, headers?: Headers) => Response
  redirect: (location: string, status?: StatusCode) => Response
  cookie: (name: string, value: string, options?: CookieOptions) => void
  notFound: () => Response | Promise<Response>
}

export class HonoContext<RequestParamKeyType extends string = string, E = Env>
  implements Context<RequestParamKeyType, E>
{
  req: Request<RequestParamKeyType>
  env: E
  finalized: boolean