@cloudflare/workers-types 0.20221111.0

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 ADDED
@@ -0,0 +1,112 @@
1
+ # Cloudflare Workers Types
2
+
3
+ > :warning: If you're upgrading from version 2, make sure to remove `webworker` from the `lib` array in your
4
+ > `tsconfig.json`. These types are now included in `@cloudflare/workers-types`.
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ npm install -D @cloudflare/workers-types
10
+ -- Or
11
+ yarn add -D @cloudflare/workers-types
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ The following is a minimal `tsconfig.json` for use alongside this package:
17
+
18
+ **`tsconfig.json`**
19
+
20
+ ```json
21
+ {
22
+ "compilerOptions": {
23
+ "target": "esnext",
24
+ "module": "esnext",
25
+ "lib": ["esnext"],
26
+ "types": ["@cloudflare/workers-types"]
27
+ }
28
+ }
29
+ ```
30
+
31
+ ### Compatibility dates
32
+
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:
34
+
35
+ - `@cloudflare/workers-types`
36
+
37
+ The default entrypoint exposes the runtime types for a compatibility date of `2021-01-01` or before.
38
+
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
+ }
69
+ }
70
+ ```
71
+
72
+ ### Importable Types
73
+
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:
75
+
76
+ ```ts
77
+ import type { Request as WorkerRequest, ExecutionContext } from "@cloudflare/workers-types/experimental"
78
+
79
+ export default {
80
+ fetch(request: WorkerRequest, env: unknown, ctx: ExecutionContext) {
81
+ return new Response("OK")
82
+ }
83
+ }
84
+ ```
85
+
86
+
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
+ ```
110
+
111
+ Wrangler can also generate this for you automatically from your `wrangler.toml` configuration file, using the `wrangler types` command.
112
+