@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/2021-11-03/index.d.ts +2794 -0
- package/2021-11-03/index.ts +2799 -0
- package/2022-01-31/index.d.ts +2782 -0
- package/2022-01-31/index.ts +2787 -0
- package/2022-03-21/index.d.ts +2788 -0
- package/2022-03-21/index.ts +2793 -0
- package/2022-08-04/index.d.ts +2789 -0
- package/2022-08-04/index.ts +2794 -0
- package/README.md +72 -35
- package/experimental/index.d.ts +2788 -0
- package/experimental/index.ts +2793 -0
- package/index.d.ts +1399 -1399
- package/index.ts +2799 -0
- package/oldest/index.d.ts +2794 -0
- package/oldest/index.ts +2799 -0
- package/package.json +4 -27
- package/LICENSE +0 -29
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": "
|
|
24
|
-
"module": "
|
|
25
|
-
"lib": ["
|
|
23
|
+
"target": "esnext",
|
|
24
|
+
"module": "esnext",
|
|
25
|
+
"lib": ["esnext"],
|
|
26
26
|
"types": ["@cloudflare/workers-types"]
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### Compatibility dates
|
|
32
32
|
|
|
33
|
-
|
|
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
|
-
|
|
35
|
+
- `@cloudflare/workers-types`
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
export {};
|
|
37
|
+
The default entrypoint exposes the runtime types for a compatibility date of `2021-01-01` or before.
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
72
|
+
### Importable Types
|
|
49
73
|
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|