Worker Env Type Generation: Bindings, Runtime, and CI Validation
How to generate TypeScript Env and runtime types from Wrangler config, handle multi-environment bindings, and keep types current in CI.
This page summarizes Cloudflare Workers documentation on generating TypeScript types for Worker projects. The recommended workflow is wrangler types: it produces an Env interface from your bindings and runtime types locked to your compatibility_date and compatibility_flags, so mismatches show up at compile time instead of at deploy time.
wrangler types: what it generates and when to run it
Section titled “wrangler types: what it generates and when to run it”wrangler types reads your Wrangler configuration and writes a declaration file (default worker-configuration.d.ts) with binding types for env and, by default, runtime API types from workerd.
Generate types based on your Worker configuration, including Env types based on your bindings, module rules, and runtime types based on the compatibility_date and compatibility_flags in your config file.
— Cloudflare, Workers Wrangler commands —
types
The correct types for a Worker depend on its compatibility date, compatibility flags, bindings in the Wrangler file, and any module rules—for example, nodejs_als must be in flags for AsyncLocalStorage to appear in generated runtime types. See TypeScript on Workers.
Cloudflare advises against hand-writing Env:
Do not hand-write your Env interface. Run wrangler types to generate a type definition file that matches your Wrangler configuration. This catches mismatches between your config and code at compile time instead of at deploy time.
— Cloudflare, Workers best practices
Re-run generation whenever you add, rename, or remove a binding, change environments, or update compatibility settings. The generated file is overwritten on each run—do not edit it manually; put custom types in source-owned files and use declaration merging with Cloudflare.Env when needed (Agents configuration — Generating types).
How to run wrangler types in a new or existing project
Section titled “How to run wrangler types in a new or existing project”-
From the project root, run:
npx wrangler typesPackage managers:
yarn wrangler types,pnpm wrangler types(TypeScript on Workers). -
Add the output to
tsconfig.json:{"compilerOptions": {"types": ["./worker-configuration.d.ts"]}}Use your custom path if you passed one on the command line (TypeScript — step 3).
-
If you use
nodejs_compat, install@types/nodeand include"node"in thetypesarray (TypeScript — optional step 4). -
Optionally add a script, e.g.
"type-check": "wrangler types && tsc"(TypeScript — CI).
For Agents, cleaner output is often:
npx wrangler types env.d.ts --include-runtime falseThat yields bindings (including Durable Object namespaces) without full runtime definitions (Agents configuration — Without runtime types).
Multi-environment bindings: all environments in one Env type
Section titled “Multi-environment bindings: all environments in one Env type”By default, wrangler types aggregates bindings from every environment in the Wrangler file, not only top-level or a single --env target.
The wrangler types command now generates TypeScript types for bindings from all environments defined in your Wrangler configuration file by default.
— Cloudflare,
wrangler typesnow generates types for all environments
Bindings that exist only in staging or production appear on the shared Env type—often as optional properties—so code that references environment-specific KV, R2, or queues does not fail TypeScript when you switch deploy targets (Workers Wrangler commands — Multi-environment support).
When to use --env for a single environment only
Section titled “When to use --env for a single environment only”To restore the older behavior (types for one environment only):
wrangler types --env productionUse this when you want a minimal Env tied to one named environment (changelog — Generating types for a specific environment).
Secrets, vars, and CI without local .dev.vars
Section titled “Secrets, vars, and CI without local .dev.vars”Type generation for secrets no longer depends on inferring names from .dev.vars or .env:
wrangler types generates typed bindings from secrets.required instead of inferring names from .dev.vars or .env. This lets you run type generation in CI or other environments where those files are not present. Per-environment secrets are supported — the aggregated Env type marks secrets that only appear in some environments as optional.
— Cloudflare, Declare required secrets — Type generation
Declare required secret names in Wrangler config so CI can run wrangler types without local secret files.
For vars bindings, --strict-vars (default true) produces literal or union types across environments; set --strict-vars false for plain string when values change often (Workers Wrangler commands — Options).
Runtime types: wrangler types vs @cloudflare/workers-types v5
Section titled “Runtime types: wrangler types vs @cloudflare/workers-types v5”Cloudflare recommends generating runtime types with wrangler types so they match your Worker’s compatibility date and flags. The npm package remains useful for libraries and shared packages in the Workers ecosystem, not as the primary app typing strategy (TypeScript — Migrating).
We still recommend that you generate types for your Worker using wrangler types, but if you want to use the package directly, you can install it with your package manager of choice
— Cloudflare, Simpler runtime types with
@cloudflare/workers-typesv5
Version 5 simplifies entrypoints:
| Entry | Purpose |
|---|---|
@cloudflare/workers-types |
Latest stable compatibility flags |
@cloudflare/workers-types/experimental |
APIs behind experimental flags |
Dated paths (e.g. 2023-07-01) |
Removed; use wrangler types for a locked runtime (v5 changelog) |
Workers Request, Response, and Headers types from generated or package definitions include runtime-specific fields (e.g. cf, getAll()) and may not match lib.dom types—type assertions or new objects may be needed when mixing contexts (Request, Headers).
wrangler types --check: validate types in CI without rewriting files
Section titled “wrangler types --check: validate types in CI without rewriting files”--check compares recorded configuration and runtime metadata in the committed declaration file with the current Wrangler config. It does not diff the full file body or regenerate it.
This flag validates that your generated types are up to date without writing any changes to disk. This is useful in CI/CD pipelines where you want to ensure that developers have regenerated their types after making changes to their Wrangler configuration. If the types are out of date, the command will exit with a non-zero status code.
— Cloudflare, Validate your generated types with
wrangler types --check
npx wrangler types --checkCommit the generated file and use --check in CI, or run wrangler types before tsc on every build (TypeScript — CI pipelines).
CLI options that matter in real projects
Section titled “CLI options that matter in real projects”| Flag / argument | Role |
|---|---|
[PATH] |
Output .d.ts path (default worker-configuration.d.ts) |
--env |
Bindings for one environment only |
--env-interface |
Interface name (e.g. ApiEnv for tests) |
--include-runtime / --include-env |
Toggle runtime or binding sections (default both true) |
--config / -c |
Multiple configs for RPC Service / DurableObjectNamespace parameter types |
--check |
CI staleness check |
RPC and service bindings: multiple -c config paths
Section titled “RPC and service bindings: multiple -c config paths”For typed RPC clients, pass the client config plus bound Worker and Durable Object configs:
npx wrangler types -c ./client/wrangler.jsonc -c ../sum-worker/wrangler.jsonc -c ../counter/wrangler.jsoncThat can produce Service<import("...").SumService> and parameterized DurableObjectNamespace types (RPC TypeScript). The test harness uses --env-interface per Worker so several worker-configuration.d.ts files can coexist in one test tsconfig.
Programmatic generation: experimental_generateTypes
Section titled “Programmatic generation: experimental_generateTypes”The Wrangler API mirrors the CLI but returns strings instead of writing disk by default:
Generate TypeScript type definitions from your Worker configuration. This API uses the same core logic as the wrangler types CLI command, so outputs stay aligned between the CLI and programmatic API.
— Cloudflare, Wrangler API —
experimental_generateTypes
The experimental_ prefix marks the API as unstable (Wrangler API). Options mirror CLI flags (env, envInterface, includeEnv, includeRuntime, path, strictVars, config, envFile).
Pages Functions, Agents, and other layouts
Section titled “Pages Functions, Agents, and other layouts”- Pages Functions:
npx wrangler types --path='./functions/types.d.ts'and a dedicatedfunctions/tsconfig.jsonwithPagesFunction<Env>(Pages Functions TypeScript). - Agents: prefer
wrangler typeswith--include-runtime false;Agentdefaults to generatedEnv(Add to existing project). - Vitest: include generated
worker-configuration.d.tsin testtsconfiginclude(Write your first test). - Vite
cfprojects:cf workers typeswrites.cloudflare/types/index.d.ts(parallel to Wrangler-first flows) (Develop, build, and deploy). - Gateway middleware on Workers: install
@cloudflare/zt-typesforGatewayMiddlewareContextand related types—separate fromEnvgeneration (Available context). - Schema’d pipeline bindings: typed
Pipeline<…>record types appear when a stream schema is defined (Writing to streams).
ctx.exports typing also relies on generated project types; entrypoints with props should use WorkerEntrypoint<Env, Props> (Context (ctx)).
Common mistakes
Section titled “Common mistakes”- Editing
worker-configuration.d.tsby hand — the nextwrangler typesrun deletes those edits; merge custom types elsewhere. - Stale types after config changes — TypeScript still compiles but deploy fails or RPC calls miss methods; regenerate and commit, or enforce
--checkin CI. - Expecting only production bindings in
Env— default aggregation includes all environments; use--envif you need a narrow interface. - Relying on
.dev.varsfor secret names in CI — usesecrets.requiredin config (secrets changelog). - Mixing DOM and Workers
Request/Responsetypes — use Workers-generated types in Worker code paths (Response differences). - Tutorial samples with manual
interface Env— older guides show hand-writtenEnv; prefer generation (Queues tutorial note vs best practices).
How to check this on your site
Section titled “How to check this on your site”- Run
npx wrangler types(or your project’stypesscript) and confirmworker-configuration.d.ts(or your custom path) lists every binding you use in code. - Run
npx wrangler types --checkin CI or locally after a config change; fix any non-zero exit by regenerating and committing (--checkchangelog). - Run
tsc(or yourtype-checkscript) and verifyenvbindings autocomplete andctx.exports/ RPC stubs type-check (TypeScript on Workers). - After changing
compatibility_dateor flags, regenerate and confirm new runtime APIs appear or disappear as expected (generate types that match configuration). - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.