Skip to content

Commit 1439676

Browse files
committed
update wrangler types runtime type gen instructions
1 parent ffb95b1 commit 1439676

File tree

5 files changed

+117
-81
lines changed

5 files changed

+117
-81
lines changed

src/content/docs/pages/functions/typescript.mdx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@ sidebar:
55
order: 8
66
---
77

8+
import { PackageManagers, Render } from "~/components";
9+
810
Pages Functions supports TypeScript. Author any files in your `/functions` directory with a `.ts` extension instead of a `.js` extension to start using TypeScript.
911

10-
To add the runtime types to your project, run:
12+
You can add runtime types and Env types by running:
1113

12-
```sh
13-
npm install --save-dev typescript @cloudflare/workers-types
14-
```
14+
<PackageManagers
15+
type="exec"
16+
pkg="wrangler"
17+
args={"types --path='./functions/types.d.ts'"}
18+
/>
1519

16-
Then configure the runtime types by creating a `functions/tsconfig.json` file:
20+
Then configure the types by creating a `functions/tsconfig.json` file:
1721

1822
```json
1923
{
2024
"compilerOptions": {
2125
"target": "esnext",
2226
"module": "esnext",
2327
"lib": ["esnext"],
24-
"types": ["@cloudflare/workers-types"]
28+
"types": ["./types.d.ts"]
2529
}
2630
}
2731
```
2832

33+
See [the `wrangler types` command docs](/workers/wrangler/commands/#types) for more details.
34+
2935
If you already have a `tsconfig.json` at the root of your project, you may wish to explicitly exclude the `/functions` directory to avoid conflicts. To exclude the `/functions` directory:
3036

3137
```json
@@ -36,7 +42,9 @@ If you already have a `tsconfig.json` at the root of your project, you may wish
3642
}
3743
```
3844

39-
Pages Functions can be typed using the `PagesFunction` type. This type accepts an `Env` parameter. To use the `env` parameter:
45+
Pages Functions can be typed using the `PagesFunction` type. This type accepts an `Env` parameter. The `Env` type should also have been generated by `wrangler types` and can be found at the top of `types.d.ts`.
46+
47+
Alternatively, you can define the `Env` type manually. For example:
4048

4149
```ts
4250
interface Env {
@@ -48,3 +56,22 @@ export const onRequest: PagesFunction<Env> = async (context) => {
4856
return new Response(value);
4957
};
5058
```
59+
60+
If you are using `nodejs_compat`, you should also make sure you have installed `@types/node` and updated your `tsconfig.json`.
61+
62+
```json
63+
{
64+
"compilerOptions": {
65+
"target": "esnext",
66+
"module": "esnext",
67+
"lib": ["esnext"],
68+
"types": ["./types.d.ts", "node"]
69+
}
70+
}
71+
```
72+
73+
:::note
74+
75+
If you were previously using `@cloudflare/workers-types` instead of the runtime types generated by `wrangler types`, you can refer to this [migration guide](/workers/languages/typescript/#migrating).
76+
77+
:::

src/content/docs/workers/languages/javascript/index.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ sidebar:
66

77
---
88

9-
## JavaScript
109

1110
The Workers platform is designed to be [JavaScript standards compliant](https://ecma-international.org/publications-and-standards/standards/ecma-262/) and web-interoperable, and supports JavaScript standards, as defined by [TC39](https://tc39.es/) (ECMAScript). Wherever possible, it uses web platform APIs, so that code can be reused across client and server, as well as across [WinterCG](https://wintercg.org/) JavaScript runtimes.
1211

src/content/docs/workers/languages/typescript/index.mdx

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,113 +8,122 @@ head:
88
content: Write Cloudflare Workers in TypeScript
99
---
1010

11-
import { TabItem, Tabs } from "~/components";
11+
import { TabItem, Tabs, PackageManagers, Render } from "~/components";
1212

13-
## TypeScript
13+
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](https://github.com/cloudflare/workerd), the open-source Workers runtime.
1414

15-
TypeScript is a first-class language on Cloudflare Workers. Cloudflare publishes type definitions to [GitHub](https://github.com/cloudflare/workers-types) and [npm](https://www.npmjs.com/package/@cloudflare/workers-types) (`npm install -D @cloudflare/workers-types`). All APIs provided in Workers are fully typed, and type definitions are generated directly from [workerd](https://github.com/cloudflare/workerd), the open-source Workers runtime.
15+
We recommend you generate types for your Worker by running [`wrangler types`](/workers/wrangler/commands/#types). Cloudflare also publishes type definitions to [GitHub](https://github.com/cloudflare/workers-types) and [npm](https://www.npmjs.com/package/@cloudflare/workers-types) (`npm install -D @cloudflare/workers-types`).
1616

17-
### Generate types that match your Worker's configuration (experimental)
17+
<h3 id="generate-types">
18+
Generate types that match your Worker's configuration
19+
</h3>
1820

1921
Cloudflare continuously improves [workerd](https://github.com/cloudflare/workerd), the open-source Workers runtime.
20-
Changes in workerd can introduce JavaScript API changes, thus changing the respective TypeScript types. For example, the [`urlsearchparams_delete_has_value_arg`](/workers/configuration/compatibility-flags/#urlsearchparams-delete-and-has-value-argument) compatibility flag adds optional arguments to some methods, in order to support new additions to the WHATWG URL standard API.
22+
Changes in workerd can introduce JavaScript API changes, thus changing the respective TypeScript types.
2123

22-
This means the correct TypeScript types for your Worker depend on:
24+
This means the correct types for your Worker depend on:
2325

24-
1. Your Worker's [Compatibility Date](/workers/configuration/compatibility-dates/).
25-
2. Your Worker's [Compatibility Flags](/workers/configuration/compatibility-flags/).
26+
1. Your Worker's [compatibility date](/workers/configuration/compatibility-dates/).
27+
2. Your Worker's [compatibility flags](/workers/configuration/compatibility-flags/).
28+
3. Your Worker's bindings, which are accessed on the `env` object.
29+
4. Any [module rules](/workers/wrangler/configuration/#inheritable-keys) you have specified in your Wrangler configuration file under `rules`.
2630

27-
For example, if you have `compatibility_flags = ["nodejs_als"]` in your `wrangler.toml`, then the runtime will allow you to use the [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) class in your worker code, but you will not see this reflected in the type definitions in `@cloudflare/workers-types`.
31+
For example, the runtime will onlly allow you to use the [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) class if you have `compatibility_flags = ["nodejs_als"]` in your [Wrangler configuration file](/workers/wrangler/configuration/). This should be reflected in the type definitions.
2832

29-
In order to solve this issue, and to ensure that your type definitions are always up-to-date with your compatibility settings, you can dynamically generate the runtime types (as of `wrangler 3.66.0`):
33+
To ensure that your type definitions always match your Worker's configuration, you can dynamically generate types by running:
3034

31-
```bash
32-
npx wrangler types --experimental-include-runtime
33-
```
35+
<PackageManagers type="exec" pkg="wrangler" args={"types"} />
3436

35-
This will generate a `d.ts` file and (by default) save it to `.wrangler/types/runtime.d.ts`. You will be prompted in the command's output to add that file to your `tsconfig.json`'s `compilerOptions.types` array.
37+
See [the `wrangler types` command docs](/workers/wrangler/commands/#types) for more details.
3638

37-
If you would like to commit the file to git, you can provide a custom path. Here, for instance, the `runtime.d.ts` file will be saved to the root of your project:
39+
:::note
3840

39-
```bash
40-
npx wrangler types --experimental-include-runtime="./runtime.d.ts"
41-
```
41+
<Render file="wrangler-commands/runtime-types" product="workers" />
4242

43-
**Note: To ensure that your types are always up-to-date, make sure to run `wrangler types --experimental-include-runtime` after any changes to your config file.**
43+
:::
4444

45-
See [the full list of available flags](/workers/wrangler/commands/#types) for more details.
45+
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.
4646

47-
#### Migrating from `@cloudflare/workers-types` to `wrangler types --experimental-include-runtime`
47+
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`.
4848

49-
The `@cloudflare/workers-types` package provides runtime types for each distinct [compatibility date](https://github.com/cloudflare/workerd/tree/main/npm/workers-types#compatibility-dates), which is specified by the user in their `tsconfig.json`. But this package is superseded by the `wrangler types --experimental-include-runtime` command.
49+
You can commit your types file to git if you wish.
5050

51-
Here are the steps to switch from `@cloudflare/workers-types` to using `wrangler types` with the experimental runtime inclusion:
51+
:::note
5252

53-
##### Uninstall `@cloudflare/workers-types`
53+
To ensure that your types are always up-to-date, make sure to run `wrangler types` after any changes to your config file.\*\*
5454

55-
<Tabs> <TabItem label="npm">
55+
:::
5656

57-
```sh
58-
npm uninstall @cloudflare/workers-types
59-
```
57+
<h3 id="migrating">
58+
Migrating from `@cloudflare/workers-types` to `wrangler types`
59+
</h3>
6060

61-
</TabItem> <TabItem label="yarn">
61+
We recommend you use `wrangler types` to generate runtime types, rather than using the `@cloudflare/workers-types` package, as it provides better support for combinations of [compatibility date](https://github.com/cloudflare/workerd/tree/main/npm/workers-types#compatibility-dates) and `compatibility flags`. Note that there are no plans to stop publishing the `@cloudflare/workers-types` package.
6262

63-
```sh
64-
yarn remove @cloudflare/workers-types
65-
```
63+
#### 1. Uninstall `@cloudflare/workers-types`
6664

67-
</TabItem> <TabItem label="pnpm">
65+
<PackageManagers type="remove" pkg="@cloudflare/workers-types" />
6866

69-
```sh
70-
pnpm uninstall @cloudflare/workers-types
71-
```
67+
#### 2. Generate runtime types using Wrangler
7268

73-
</TabItem> <TabItem label="bun">
69+
<PackageManagers type="exec" pkg="wrangler" args={"types"} />
7470

75-
```sh
76-
bun remove @cloudflare/workers-types
77-
```
71+
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`.
7872

79-
</TabItem> </Tabs>
73+
You can now remove any imports from `@cloudflare/workers-types` in your Worker code.
8074

81-
##### Generate runtime types using wrangler
75+
:::note
8276

83-
```bash
84-
npx wrangler types --experimental-include-runtime
85-
```
77+
<Render file="wrangler-commands/runtime-types" product="workers" />
8678

87-
This will generate a `.d.ts` file, saved to `.wrangler/types/runtime.d.ts` by default.
79+
:::
8880

89-
##### Update your `tsconfig.json` to include the generated types
81+
#### 3. Make sure your `tsconfig.json` includes the generated types
9082

9183
```json
9284
{
9385
"compilerOptions": {
94-
"types": ["./.wrangler/types/runtime"]
86+
"types": ["worker-configuration.d.ts"]
9587
}
9688
}
9789
```
9890

9991
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.
10092

101-
##### Update your scripts and CI pipelines
93+
#### 4. Add @types/node if you are using [`nodejs_compat`](/workers/runtime-apis/nodejs/) (Optional)
10294

103-
When switching to `wrangler types --experimental-include-runtime`, you'll want to ensure that your development process always uses the most up-to-date types. The main thing to remember here is that - regardless of your specific framework or build tools - you should run the `wrangler types --experimental-include-runtime` command before any development tasks that rely on TypeScript. This ensures your editor and build tools always have access to the latest types.
95+
If you are using the `nodejs_compat` compatibility flag, you should also install `@types/node`.
10496

105-
Most projects will have existing build and development scripts scripts, as well as some type-checking. In the example below, we're adding the `wrangler types --experimental-include-runtime` before the type-checking script in the project:
97+
<PackageManagers type="add" pkg="@types/node" />
98+
99+
Then add this to your `tsconfig.json`.
100+
101+
```json
102+
{
103+
"compilerOptions": {
104+
"types": ["worker-configuration.d.ts", "node"]
105+
}
106+
}
107+
```
108+
109+
#### 5. Update your scripts and CI pipelines
110+
111+
Regardless of your specific framework or build tools, you should run the `wrangler types` command before any tasks that rely on TypeScript.
112+
113+
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:
106114

107115
```json
108116
{
109117
"scripts": {
110118
"dev": "existing-dev-command",
111-
"build": "-existing-build-command",
112-
"type-check": "wrangler types --experimental-include-runtime && tsc"
119+
"build": "existing-build-command",
120+
"generate-types": "wrangler types",
121+
"type-check": "generate-types && tsc"
113122
}
114123
}
115124
```
116125

117-
In CI, you may have a separate build and test commands. It is best practice to run `wrangler types --experimental-include-runtime` before other CI commands. For example:
126+
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:
118127

119128
<Tabs> <TabItem label="npm">
120129

@@ -150,8 +159,6 @@ In CI, you may have a separate build and test commands. It is best practice to r
150159
151160
</TabItem> </Tabs>
152161
153-
By integrating the `wrangler types --experimental-include-runtime` command into your workflow this way, you ensure that your development environment, builds, and CI processes always use the most accurate and up-to-date types for your Cloudflare Worker, regardless of your specific framework or tooling choices.
154-
155162
### Known issues
156163
157164
#### Transitive loading of `@types/node` overrides `@cloudflare/workers-types`
@@ -176,3 +183,4 @@ For more information, refer to [this GitHub issue](https://github.com/cloudflare
176183
- [@cloudflare/workers-types](https://github.com/cloudflare/workers-types)
177184
- [Runtime APIs](/workers/runtime-apis/)
178185
- [TypeScript Examples](/workers/examples/?languages=TypeScript)
186+
- [test link](/workers/languages/typescript/#type-short)

src/content/docs/workers/wrangler/commands.mdx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ wrangler queues info <name>
17501750
```
17511751

17521752
- `name` <Type text="string" /> <MetaInfo text="required" />
1753-
- The name of the queue to inspect.
1753+
- The name of the queue to inspect.
17541754

17551755
### `consumer`
17561756

@@ -2256,37 +2256,32 @@ Deleted certificate 99f5fef1-6cc1-46b8-bd79-44a0d5082b8d successfully
22562256

22572257
## `types`
22582258

2259-
Generate types from bindings and module rules in configuration.
2259+
Generate types based on your Worker configuration, including `Env` types based on your bindings, module rules, and [runtime types](/workers/language/typescript/) based on the`compatibility_date` and `compatibility_flags` in your [config file](/workers/wrangler/configuration/).
22602260

22612261
```txt
22622262
wrangler types [<PATH>] [OPTIONS]
22632263
```
22642264

22652265
:::note
22662266

2267-
The `--experimental-include-runtime` flag dynamically generates runtime types according to the `compatibility_date` and `compatibility_flags` defined in your [config file](/workers/wrangler/configuration/).
2268-
2269-
It is a replacement for the [`@cloudflare/workers-types` package](https://www.npmjs.com/package/@cloudflare/workers-types), so that package, if installed, should be uninstalled to avoid any potential conflict.
2270-
2271-
After running the command, you must add the path of the generated runtime types file to the [`compilerOptions.types` field](https://www.typescriptlang.org/tsconfig/#types) in your project's [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) file.
2272-
2273-
You may use the shorthand `--x-include-runtime` flag in place of `--experimental-include-runtime` anywhere it is mentioned.
2274-
2275-
The minimum required Wrangler version to use this command is 3.66.0.
2267+
<Render file="wrangler-commands/runtime-types" product="workers" />
22762268

22772269
:::
22782270

22792271
- `PATH` <Type text="string" /> <MetaInfo text="(default: `./worker-configuration.d.ts`)" />
2280-
- The path to where **the `Env` types** for your Worker will be written.
2272+
- The path to where types for your Worker will be written.
22812273
- The path must have a `d.ts` extension.
22822274
- `--env-interface` <Type text="string" /> <MetaInfo text="(default: `Env`)" />
22832275
- The name of the interface to generate for the environment object.
22842276
- Not valid if the Worker uses the Service Worker syntax.
2285-
- `--experimental-include-runtime` <Type text="string" /> <MetaInfo text="optional (default: `./.wrangler/types/runtime.d.ts`)" />
2286-
- The path to where the **runtime types** file will be written.
2287-
- Leave the path blank to use the default option, e.g. `npx wrangler types --x-include-runtime`
2288-
- A custom path must be relative to the project root, e.g. `./my-runtime-types.d.ts`
2289-
- A custom path must have a `d.ts` extension.
2277+
- `--include-runtime` <Type text="boolean" /> <MetaInfo text="(default: true)" />
2278+
- Whether to generate runtime types based on the`compatibility_date` and `compatibility_flags` in your [config file](/workers/wrangler/configuration/).
2279+
- `--include-env` <Type text="boolean" /> <MetaInfo text="(default: true)" />
2280+
- Whether to generate `Env` types based on your Worker bindings.
2281+
- `--strict-vars` <Type text="boolean" /> <MetaInfo text="optional (default: true)" />
2282+
- Control the types that Wrangler generates for `vars` bindings.
2283+
- If `true`, (the default) Wrangler generates literal and union types for bindings (e.g. `myVar: 'my dev variable' | 'my prod variable'`).
2284+
- If `false`, Wrangler generates generic types (e.g. `myVar: string`). This is useful when variables change frequently, especially when working across multiple environments.
22902285

22912286
<Render file="wrangler-commands/global-flags" product="workers" />
22922287

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
{}
3+
---
4+
5+
import { Type } from "~/components";
6+
7+
If you are running a version of Wrangler that is greater than `3.66.0` but below `4.0.0`, you will need to include the `--experimental-include-runtime` flag. During its experimental release, runtime types were output to a separate file (`.wrangler/types/runtime.d.ts` by default). If you have an older version of Wrangler, you can access runtime types through the `@cloudflare/workers-types` package.

0 commit comments

Comments
 (0)