|
1 | 1 | # Cloudflare Workers Types
|
2 | 2 |
|
3 |
| -**Install** |
| 3 | +## Install |
4 | 4 |
|
5 | 5 | ```bash
|
6 |
| -npm install --save-dev @cloudflare/workers-types |
| 6 | +npm install @cloudflare/workers-types |
7 | 7 | -- Or
|
8 |
| -yarn add -D @cloudflare/workers-types |
| 8 | +yarn add @cloudflare/workers-types |
9 | 9 | ```
|
10 | 10 |
|
11 |
| -**Usage** |
| 11 | +## Usage |
12 | 12 |
|
13 |
| -Just supply an empty import in one of your source files to receive the workers types |
| 13 | +The following is a minimal `tsconfig.json` for use alongside this package: |
14 | 14 |
|
15 |
| -```typescript |
16 |
| -import {} from '@cloudflare/workers-types' |
17 |
| -``` |
| 15 | +**`tsconfig.json`** |
18 | 16 |
|
19 |
| -Make sure that the `DOM`, `DOM.Iterable` and `WebWorker` [libraries][tsconfig-lib] are included in your [`tsconfig.json`]. e.g.: |
20 | 17 | ```json
|
21 |
| -"lib": ["DOM", "DOM.Iterable", "WebWorker"] // As well as "ESNext", etc. as your project requires |
| 18 | +{ |
| 19 | + "compilerOptions": { |
| 20 | + "target": "ES2020", |
| 21 | + "module": "CommonJS", |
| 22 | + "lib": ["ES2020", "WebWorker"], |
| 23 | + "types": ["@cloudflare/workers-types"] |
| 24 | + } |
| 25 | +} |
22 | 26 | ```
|
23 |
| -`@cloudflare/workers-types` definitions are merged with `WebWorker` while `DOM` and `DOM.Iterable` are used in the [`Request`], [`Response`] and [`Headers`] interfaces. |
24 |
| - |
25 | 27 |
|
26 |
| -### Using a KV namespace |
| 28 | +### Using bindings |
27 | 29 |
|
28 |
| -It's recommended that you create an ambient type file for your KV namespace bindings. Create a file named `types.d.ts` in your src directory: |
| 30 | +It's recommended that you create an ambient type file for any bindings your Worker uses. Create a file named `bindings.d.ts` in your src directory: |
29 | 31 |
|
30 |
| -**`types.d.ts`** |
| 32 | +**`bindings.d.ts`** |
31 | 33 |
|
32 | 34 | ```typescript
|
33 |
| -import { KVNamespace } from '@cloudflare/workers-types' |
34 |
| - |
35 | 35 | declare global {
|
| 36 | + const MY_ENV_VAR: string |
| 37 | + const MY_SECRET: string |
36 | 38 | const myKVNamespace: KVNamespace
|
37 | 39 | }
|
38 | 40 | ```
|
39 |
| - |
40 |
| -Now `myKVNamespace` is available to all of your source files. |
41 |
| - |
42 |
| - |
43 |
| -[tsconfig-lib]: https://www.typescriptlang.org/tsconfig#lib |
44 |
| -[`tsconfig.json`]: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html |
45 |
| -[`Request`]: https://developers.cloudflare.com/workers/reference/apis/request/ |
46 |
| -[`Response`]: https://developers.cloudflare.com/workers/reference/apis/response/ |
47 |
| -[`Headers`]: https://developer.mozilla.org/en-US/docs/Web/API/Headers |
0 commit comments