/Knowledge/sources/honojs__hono/llms-full/context-contextvariablemap--103c83b3a62e.md
Context: ContextVariableMap - hono-docs
Context: ContextVariableMap Source evidence: /Sources/honojs hono/provenance.md Canonical citation: https://hono.dev/llms full.txt contextvariablemap Summary...
Context: ContextVariableMap
Source evidence: /Sources/honojs__hono/provenance.md Canonical citation: https://hono.dev/llms-full.txt#contextvariablemap
Summary
::: warning ContextVariableMap adds types globally to all contexts, regardless of whether the middleware that sets the variable has actually run. This means c.get('result') will appear type-safe even in handlers where your middle...
Content
::: warning
ContextVariableMap adds types globally to all contexts, regardless of whether the middleware that sets the variable has actually run. This means c.get('result') will appear type-safe even in handlers where your middleware was never registered, potentially hiding undefined bugs at runtime.
Take a look at the following example:
declare module 'hono' {
interface ContextVariableMap {
result: string
}
}
const mw = createMiddleware(async (c, next) => {
c.set('result', 'some values')
await next()
})
const app = new Hono()
// handler uses the middleware
app.get('/foo', mw, (c) => {
const val = c.get('result') // ✅ val is a string and typed as such, as expected
})
// handler doesn't use the middleware
app.get('/bar', (c) => {
const val = c.get('result') // ❌ val is undefined but typed as a string, which can lead to runtime errors
})
:::
Metadata
{
"chunk_index": 0,
"citation": "https://hono.dev/llms-full.txt#contextvariablemap",
"coverage_role": "overview",
"qualified_title": "Hono / llms-full / Context > ContextVariableMap (1)",
"retrieved_at": "2026-07-02T05:51:01Z",
"section_index": 568,
"source_id": "/honojs/hono",
"source_type": "llms_full",
"target_label": "llms-full",
"title": "Context: ContextVariableMap",
"upstream_url": "https://hono.dev/llms-full.txt",
"version": "2026"
}