TypeScript
TypeScript is a first-class language on Cloudflare Workers. All APIs provided in Workers are fully typed, and type definitions are generated directly from workerd ↗, the open-source Workers runtime.
We recommend you generate types for your Worker by running wrangler types
. Cloudflare also publishes type definitions to GitHub ↗ and npm ↗ (npm install -D @cloudflare/workers-types
).
Generate types that match your Worker's configuration
Cloudflare continuously improves workerd ↗, the open-source Workers runtime. Changes in workerd can introduce JavaScript API changes, thus changing the respective TypeScript types.
This means the correct types for your Worker depend on:
- Your Worker's compatibility date.
- Your Worker's compatibility flags.
- Your Worker's bindings, which are defined in your Wrangler configuration file.
- Any module rules you have specified in your Wrangler configuration file under
rules
.
For example, the runtime will only allow you to use the AsyncLocalStorage
↗ class if you have compatibility_flags = ["nodejs_als"]
in your Wrangler configuration file. This should be reflected in the type definitions.
To ensure that your type definitions always match your Worker's configuration, you can dynamically generate types by running:
npx wrangler types
pnpm wrangler types
yarn wrangler types
See the wrangler types
command docs for more details.
This will generate a d.ts
file and (by default) save it to worker-configuration.d.ts
. This will include Env
types based on your Worker bindings and runtime types based on your Worker's compatibility date and flags.
You should then add that file to your tsconfig.json
's compilerOptions.types
array. If you have the nodejs_compat
compatibility flag, you should also install @types/node
.
You can commit your types file to git if you wish.
Migrating from @cloudflare/workers-types
to wrangler types
We recommend you use wrangler types
to generate runtime types, rather than using the @cloudflare/workers-types
package, as it generates types based on your Worker's compatibility date ↗ and compatibility flags
, ensuring that types match the exact runtime APIs made available to your Worker.
npm uninstall @cloudflare/workers-types
pnpm remove @cloudflare/workers-types
yarn remove @cloudflare/workers-types
npx wrangler types
pnpm wrangler types
yarn wrangler types
This will generate a .d.ts
file, saved to worker-configuration.d.ts
by default. This will also generate Env
types. If for some reason you do not want to include those, you can set --include-env=false
.
You can now remove any imports from @cloudflare/workers-types
in your Worker code.
{ "compilerOptions": { "types": ["worker-configuration.d.ts"] }}
Note that if you have specified a custom path for the runtime types file, you should use that in your compilerOptions.types
array instead of the default path.
4. Add @types/node if you are using nodejs_compat
(Optional)
If you are using the nodejs_compat
compatibility flag, you should also install @types/node
.
npm i @types/node
pnpm add @types/node
yarn add @types/node
Then add this to your tsconfig.json
.
{ "compilerOptions": { "types": ["worker-configuration.d.ts", "node"] }}
Regardless of your specific framework or build tools, you should run the wrangler types
command before any tasks that rely on TypeScript.
Most projects will have existing build and development scripts, as well as some type-checking. In the example below, we're adding the wrangler types
before the type-checking script in the project:
{ "scripts": { "dev": "existing-dev-command", "build": "existing-build-command", "generate-types": "wrangler types", "type-check": "generate-types && tsc" }}
We recommend you commit your generated types file for use in CI. Alternatively, you can run wrangler types
before other CI commands, as it should not take more than a few seconds. For example:
- run: npm run generate-types- run: npm run build- run: npm test
- run: yarn generate-types- run: yarn build- run: yarn test
- run: pnpm run generate-types- run: pnpm run build- run: pnpm test
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark