Cloudflare Hono Env Typing
Quick start
Use the Env type generated by wrangler types from apps/api/worker-configuration.d.ts, then pass it into Hono's generics.
code
import type { Env } from "../worker-configuration";
import { Hono } from "hono";
export const domainWorkersRouter = new Hono<{ Bindings: Env }>();
Workflow
- •Generate or update typings:
- •Run
wrangler typesinapps/api(this creates/updatesapps/api/worker-configuration.d.ts).
- •Run
- •Import the Env type from
apps/api/worker-configuration.d.ts:- •
import type { Env } from "../worker-configuration";
- •
- •Thread Env into Hono:
- •
new Hono<{ Bindings: Env }>()
- •
- •Use typed bindings via
c.env:- •
const db = c.env.ZEROSPIN_DATABASE
- •
Notes
- •Keep the Env import as a type-only import.
- •The
worker-configuration.d.tsfile is generated; do not hand-edit it. - •If the file path changes, update the import path in routes/modules accordingly.
Example route snippet
code
import type { Env } from "../worker-configuration";
import { Hono } from "hono";
export const domainWorkersRouter = new Hono<{ Bindings: Env }>();
domainWorkersRouter.get("/", (c) => {
const db = c.env.ZEROSPIN_DATABASE;
return c.json({ ok: true, hasDb: Boolean(db) });
});