npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cloudflare/workers-types

v4.20241112.0

Published

TypeScript typings for Cloudflare Workers

Downloads

2,563,658

Readme

Cloudflare Workers Types

Install

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

Usage

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"]
  }
}

Compatibility dates

Entrypoints for compatibility dates

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:

  • @cloudflare/workers-types

    The default entrypoint exposes the runtime types for a compatibility date before 2021-11-03.

  • @cloudflare/workers-types/2021-11-03

    This entrypoint exposes the runtime types for a compatibility date between 2021-11-03 and 2022-01-31.

  • @cloudflare/workers-types/2022-01-31

    This entrypoint exposes the runtime types for a compatibility date between 2022-01-31 and 2022-03-21.

  • @cloudflare/workers-types/2022-03-21

    This entrypoint exposes the runtime types for a compatibility date between 2022-03-21 and 2022-08-04.

  • @cloudflare/workers-types/2022-08-04

    This entrypoint exposes the runtime types for a compatibility date between 2022-08-04 and 2022-10-31.

  • @cloudflare/workers-types/2022-10-31

    This entrypoint exposes the runtime types for a compatibility date between 2022-10-31 and 2022-11-30.

  • @cloudflare/workers-types/2022-11-30

    This entrypoint exposes the runtime types for a compatibility date between 2022-11-30 and 2023-03-01.

  • @cloudflare/workers-types/2023-03-01

    This entrypoint exposes the runtime types for a compatibility date between 2023-03-01 and 2023-07-01.

  • @cloudflare/workers-types/2023-07-01

    This entrypoint exposes the runtime types for a compatibility date after 2023-07-01.

  • @cloudflare/workers-types/experimental

    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.

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"]
  }
}

Importable Types

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")
  }
}

Using bindings

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.