Skip to content

Commit af8c03e

Browse files
committed
update README usage instructions to recommend inclusion via tsconfig.json
1 parent 64e35c8 commit af8c03e

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

README.md

+19-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
11
# Cloudflare Workers Types
22

3-
**Install**
3+
## Install
44

55
```bash
6-
npm install --save-dev @cloudflare/workers-types
6+
npm install @cloudflare/workers-types
77
-- Or
8-
yarn add -D @cloudflare/workers-types
8+
yarn add @cloudflare/workers-types
99
```
1010

11-
**Usage**
11+
## Usage
1212

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:
1414

15-
```typescript
16-
import {} from '@cloudflare/workers-types'
17-
```
15+
**`tsconfig.json`**
1816

19-
Make sure that the `DOM`, `DOM.Iterable` and `WebWorker` [libraries][tsconfig-lib] are included in your [`tsconfig.json`]. e.g.:
2017
```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+
}
2226
```
23-
`@cloudflare/workers-types` definitions are merged with `WebWorker` while `DOM` and `DOM.Iterable` are used in the [`Request`], [`Response`] and [`Headers`] interfaces.
24-
2527

26-
### Using a KV namespace
28+
### Using bindings
2729

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:
2931

30-
**`types.d.ts`**
32+
**`bindings.d.ts`**
3133

3234
```typescript
33-
import { KVNamespace } from '@cloudflare/workers-types'
34-
3535
declare global {
36+
const MY_ENV_VAR: string
37+
const MY_SECRET: string
3638
const myKVNamespace: KVNamespace
3739
}
3840
```
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

tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"compilerOptions": {
33
"outDir": "./dist",
4-
"module": "commonjs",
5-
"target": "esnext",
6-
"lib": ["esnext", "webworker"],
4+
"module": "CommonJS",
5+
"target": "ES2020",
6+
"lib": ["ES2020", "WebWorker"],
77
"alwaysStrict": true,
88
"strict": true,
99
"preserveConstEnums": true,

0 commit comments

Comments
 (0)