A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/@cloudflare/workers-types below:

@cloudflare/workers-types - npm

Note

We now recommend using the Wrangler CLI and the wrangler types command to generate types based on your compatibility date and compatibility flags. You can learn more about this, and how to migrate from @cloudflare/workers-types here in our docs.

@cloudflare/workers-types will continue to be published on the same schedule.

npm install -D @cloudflare/workers-types
-- Or
yarn add -D @cloudflare/workers-types

The following is a minimal tsconfig.json for use alongside this package:

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["esnext"],
    "types": ["@cloudflare/workers-types"]
  }
}

The Cloudflare Workers runtime manages backwards compatibility through the use of 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 the following entrypoints to choose from:

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 2022-08-04 entrypoint.

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["esnext"],
    "types": ["@cloudflare/workers-types/2022-08-04"]
  }
}

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 its types, which are usable with no additional tsconfig.json setup. For example:

import type { Request as WorkerRequest, ExecutionContext } from "@cloudflare/workers-types/experimental"

export default {
  fetch(request: WorkerRequest, env: unknown, ctx: ExecutionContext) {
    return new Response("OK")
  }
}

It's recommended that you create a type file for any bindings your Worker uses. Create a file named worker-configuration.d.ts in your src directory.

If you're using Module Workers, it should look like this:

// worker-configuration.d.ts
interface Env {
  MY_ENV_VAR: string;
  MY_SECRET: string;
  myKVNamespace: KVNamespace;
}

For Service Workers, it should augment the global scope:

// worker-configuration.d.ts
declare global {
  const MY_ENV_VAR: string;
  const MY_SECRET: string;
  const myKVNamespace: KVNamespace;
}
export {}

Wrangler can also generate this for you automatically from your wrangler.toml configuration file, using the wrangler types command.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4