@cloudflare/workers-types 3.18.0 → 4.20221111.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,57 +20,94 @@ The following is a minimal `tsconfig.json` for use alongside this package:
20
20
  ```json
21
21
  {
22
22
  "compilerOptions": {
23
- "target": "ES2020",
24
- "module": "CommonJS",
25
- "lib": ["ES2020"],
23
+ "target": "esnext",
24
+ "module": "esnext",
25
+ "lib": ["esnext"],
26
26
  "types": ["@cloudflare/workers-types"]
27
27
  }
28
28
  }
29
29
  ```
30
30
 
31
- ### Using bindings
31
+ ### Compatibility dates
32
32
 
33
- It's recommended that you create an ambient type file for any bindings your Worker uses. Create a file named
34
- `bindings.d.ts` in your src directory:
33
+ The Cloudflare Workers runtime manages backwards compatibility through the use of [Compatibility Dates](https://developers.cloudflare.com/workers/platform/compatibility-dates/). Using different compatibility dates affects the runtime types available to your Worker, and so it's important you specify the correct entrypoint to the `workers-types` package to match your compatibility date (which is usually set in your `wrangler.toml` configuration file). `workers-types` currently exposes 7 entrypoints to choose from:
35
34
 
36
- **`bindings.d.ts`**
35
+ - `@cloudflare/workers-types`
37
36
 
38
- ```typescript
39
- export {};
37
+ The default entrypoint exposes the runtime types for a compatibility date of `2021-01-01` or before.
40
38
 
41
- declare global {
42
- const MY_ENV_VAR: string;
43
- const MY_SECRET: string;
44
- const myKVNamespace: KVNamespace;
39
+ - `@cloudflare/workers-types/2021-11-03`
40
+
41
+ This entrypoint exposes the runtime types for a compatibility date between `2021-01-01` and `2021-11-03`.
42
+
43
+ - `@cloudflare/workers-types/2022-01-31`
44
+
45
+ This entrypoint exposes the runtime types for a compatibility date between `2021-11-03` and `2022-01-31`.
46
+
47
+ - `@cloudflare/workers-types/2022-03-21`
48
+
49
+ This entrypoint exposes the runtime types for a compatibility date between `2022-01-31` and `2022-03-21`.
50
+
51
+ - `@cloudflare/workers-types/2022-08-04`
52
+
53
+ This entrypoint exposes the runtime types for a compatibility date between `2022-03-21` and `2022-08-04`.
54
+
55
+ - `@cloudflare/workers-types/experimental`
56
+
57
+ This entrypoint exposes the runtime types for the latest compatibility date. The types exposed by this entrypoint will change over time to always reflect the latest version of the Workers runtime.
58
+
59
+ To use one of these entrypoints, you need to specify them in your `tsconfig.json`. For example, this is a sample `tsconfig.json` for using the `experimental` entrypoint.
60
+
61
+ ```json
62
+ {
63
+ "compilerOptions": {
64
+ "target": "esnext",
65
+ "module": "esnext",
66
+ "lib": ["esnext"],
67
+ "types": ["@cloudflare/workers-types/experimental"]
68
+ }
45
69
  }
46
70
  ```
47
71
 
48
- ## Auto-Generation
72
+ ### Importable Types
49
73
 
50
- Types are automatically generated from the Workers runtime's source code on every release. However, these generated
51
- types don't include any generics or overloads, so to improve ergonomics, some of them are overridden.
74
+ It's not always possible (or desirable) to modify the `tsconfig.json` settings for a project to include all the Cloudflare Workers types. For use cases like that, this package provides importable versions of it's types, which are usable with no additional `tsconfig.json` setup. For example:
52
75
 
53
- The [`overrides`](./overrides) directory contains partial TypeScript declarations. If an override has a different type
54
- classification than its generated counterpart – for example, an `interface` is declared to override a generated `class`
55
- definition – then the override is used instead of the generated output. However, if they're the same type classification
56
- (e.g. both the override and the generated output are using `class`), then their member properties are merged:
76
+ ```ts
77
+ import type { Request as WorkerRequest, ExecutionContext } from "@cloudflare/workers-types/experimental"
57
78
 
58
- - Members in the override but not in the generated type are included in the output
59
- - If a member in the override has the same name as one in the generated type, the generated one is removed from the
60
- output, and the override is included instead
61
- - If the member is declared type `never` in the override, it is removed from the output
79
+ export default {
80
+ fetch(request: WorkerRequest, env: unknown, ctx: ExecutionContext) {
81
+ return new Response("OK")
82
+ }
83
+ }
84
+ ```
62
85
 
63
- If a named type override is declared as a type alias to `never`, that named type is removed from the output.
64
86
 
65
- JSDoc comments from overrides will also be copied over to the output.
87
+ ### Using bindings
88
+
89
+ It's recommended that you create a type file for any bindings your Worker uses. Create a file named
90
+ `worker-configuration.d.ts` in your src directory.
91
+
92
+ If you're using Module Workers, it should look like this:
93
+ ```typescript
94
+ // worker-configuration.d.ts
95
+ interface Env {
96
+ MY_ENV_VAR: string;
97
+ MY_SECRET: string;
98
+ myKVNamespace: KVNamespace;
99
+ }
100
+ ```
101
+ For Service Workers, it should augment the global scope:
102
+ ```typescript
103
+ // worker-configuration.d.ts
104
+ declare global {
105
+ const MY_ENV_VAR: string;
106
+ const MY_SECRET: string;
107
+ const myKVNamespace: KVNamespace;
108
+ }
109
+ export {}
110
+ ```
66
111
 
67
- Comment overrides can also be written in Markdown. The [`docs`](./docs) directory contains these overrides.
68
- 2<sup>nd</sup> level headings are the names of top level declarations (e.g. <code>## \`KVNamespace\`</code>),
69
- 3<sup>rd</sup> level headings are for member names (e.g. <code>### \`KVNamespace#put\`</code>), and 4<sup>th</sup> level
70
- headings correspond to JSDoc sections for members:
112
+ Wrangler can also generate this for you automatically from your `wrangler.toml` configuration file, using the `wrangler types` command.
71
113
 
72
- - `#### Parameters`: a list with parameters of the form <code>- \`param\`: param description</code>, these will be
73
- formatted as `@param` tags
74
- - `#### Returns`: contents will be copied as-is to the `@returns` tag
75
- - `#### Examples`: fenced code blocks with the language set to `js`, `ts`, `javascript` or `typescript` will be copied
76
- to `@example` tags