Skip to content

Commit 228d918

Browse files
committed
update runtime types instructions
1 parent 5519660 commit 228d918

File tree

5 files changed

+125
-79
lines changed

5 files changed

+125
-79
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 short [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: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,107 +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 configuration file](/workers/wrangler/configuration/), 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> </Tabs>
69+
<PackageManagers type="exec" pkg="wrangler" args={"types"} />
7470

75-
##### Generate runtime types using wrangler
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`.
7672

77-
```bash
78-
npx wrangler types --experimental-include-runtime
79-
```
73+
You can now remove any imports from `@cloudflare/workers-types` in your Worker code.
74+
75+
:::note
76+
77+
<Render file="wrangler-commands/runtime-types" product="workers" />
8078

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

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

8583
```json
8684
{
8785
"compilerOptions": {
88-
"types": ["./.wrangler/types/runtime"]
86+
"types": ["worker-configuration.d.ts"]
8987
}
9088
}
9189
```
9290

9391
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.
9492

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

97-
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`.
9896

99-
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 --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:
100114

101115
```json
102116
{
103117
"scripts": {
104118
"dev": "existing-dev-command",
105-
"build": "-existing-build-command",
106-
"type-check": "wrangler types --experimental-include-runtime && tsc"
119+
"build": "existing-build-command",
120+
"generate-types": "wrangler types",
121+
"type-check": "generate-types && tsc"
107122
}
108123
}
109124
```
110125

111-
In CI, you may have 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:
112127

113128
<Tabs> <TabItem label="npm">
114129

@@ -136,8 +151,6 @@ In CI, you may have separate build and test commands. It is best practice to run
136151
137152
</TabItem> </Tabs>
138153
139-
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.
140-
141154
### Known issues
142155
143156
#### Transitive loading of `@types/node` overrides `@cloudflare/workers-types`
@@ -162,3 +175,4 @@ For more information, refer to [this GitHub issue](https://github.com/cloudflare
162175
- [@cloudflare/workers-types](https://github.com/cloudflare/workers-types)
163176
- [Runtime APIs](/workers/runtime-apis/)
164177
- [TypeScript Examples](/workers/examples/?languages=TypeScript)
178+
- [test link](/workers/languages/typescript/#type-short)

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ head:
77
description: Create, develop, and deploy your Cloudflare Workers with Wrangler commands.
88
---
99

10-
import { TabItem, Tabs, Render, Type, MetaInfo, WranglerConfig } from "~/components";
10+
import {
11+
TabItem,
12+
Tabs,
13+
Render,
14+
Type,
15+
MetaInfo,
16+
WranglerConfig,
17+
} from "~/components";
1118

1219
Wrangler offers a number of commands to manage your Cloudflare Workers.
1320

@@ -1854,6 +1861,7 @@ wrangler cert upload certificate-authority --ca-cert <PATH> [OPTIONS]
18541861
```
18551862

18561863
- `--ca-cert` <Type text="string" /> <MetaInfo text="required" />
1864+
18571865
- A path to the Certificate Authority (CA) chain certificate to upload.
18581866

18591867
- `--name` <Type text="string" /> <MetaInfo text="optional" />
@@ -1937,41 +1945,32 @@ Deleted certificate 99f5fef1-6cc1-46b8-bd79-44a0d5082b8d successfully
19371945

19381946
## `types`
19391947

1940-
Generate types from bindings and module rules in configuration.
1948+
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/).
19411949

19421950
```txt
19431951
wrangler types [<PATH>] [OPTIONS]
19441952
```
19451953

19461954
:::note
19471955

1948-
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/).
1949-
1950-
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.
1951-
1952-
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.
1953-
1954-
You may use the shorthand `--x-include-runtime` flag in place of `--experimental-include-runtime` anywhere it is mentioned.
1955-
1956-
The minimum required Wrangler version to use this command is 3.66.0.
1956+
<Render file="wrangler-commands/runtime-types" product="workers" />
19571957

19581958
:::
19591959

19601960
- `PATH` <Type text="string" /> <MetaInfo text="(default: `./worker-configuration.d.ts`)" />
1961-
- The path to where **the `Env` types** for your Worker will be written.
1961+
- The path to where types for your Worker will be written.
19621962
- The path must have a `d.ts` extension.
19631963
- `--env-interface` <Type text="string" /> <MetaInfo text="(default: `Env`)" />
19641964
- The name of the interface to generate for the environment object.
19651965
- Not valid if the Worker uses the Service Worker syntax.
1966-
- `--experimental-include-runtime` <Type text="string" /> <MetaInfo text="optional (default: `./.wrangler/types/runtime.d.ts`)" />
1967-
- The path to where the **runtime types** file will be written.
1968-
- Leave the path blank to use the default option, e.g. `npx wrangler types --x-include-runtime`
1969-
- A custom path must be relative to the project root, e.g. `./my-runtime-types.d.ts`
1970-
- A custom path must have a `d.ts` extension.
1966+
- `--include-runtime` <Type text="boolean" /> <MetaInfo text="(default: true)" />
1967+
- Whether to generate runtime types based on the`compatibility_date` and `compatibility_flags` in your [config file](/workers/wrangler/configuration/).
1968+
- `--include-env` <Type text="boolean" /> <MetaInfo text="(default: true)" />
1969+
- Whether to generate `Env` types based on your Worker bindings.
19711970
- `--strict-vars` <Type text="boolean" /> <MetaInfo text="optional (default: true)" />
19721971
- Control the types that Wrangler generates for `vars` bindings.
1973-
- If `true`, (the default) Wrangler generates literal and union types for bindings (e.g. `myEnv: 'my dev variable' | 'my prod variable'`).
1974-
- If `false`, Wrangler generates generic types (e.g. `myEnv: string`). This is useful when variables change frequently, especially when working across multiple environments.
1972+
- If `true`, (the default) Wrangler generates literal and union types for bindings (e.g. `myVar: 'my dev variable' | 'my prod variable'`).
1973+
- If `false`, Wrangler generates generic types (e.g. `myVar: string`). This is useful when variables change frequently, especially when working across multiple environments.
19751974

19761975
<Render file="wrangler-commands/global-flags" product="workers" />
19771976

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)