@khulnasoft/workers-types v4.20221111.1
Khulnasoft Workers Types
:warning: If you're upgrading from version 2, make sure to remove
webworkerfrom thelibarray in yourtsconfig.json. These types are now included in@khulnasoft/workers-types.
Install
npm install -D @khulnasoft/workers-types
-- Or
yarn add -D @khulnasoft/workers-typesUsage
The following is a minimal tsconfig.json for use alongside this package:
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"types": ["@khulnasoft/workers-types"]
}
}Compatibility dates
The Khulnasoft 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 7 entrypoints to choose from:
@khulnasoft/workers-typesThe default entrypoint exposes the runtime types for a compatibility date of
2021-01-01or before.@khulnasoft/workers-types/2021-11-03This entrypoint exposes the runtime types for a compatibility date between
2021-01-01and2021-11-03.@khulnasoft/workers-types/2022-01-31This entrypoint exposes the runtime types for a compatibility date between
2021-11-03and2022-01-31.@khulnasoft/workers-types/2022-03-21This entrypoint exposes the runtime types for a compatibility date between
2022-01-31and2022-03-21.@khulnasoft/workers-types/2022-08-04This entrypoint exposes the runtime types for a compatibility date between
2022-03-21and2022-08-04.@khulnasoft/workers-types/experimentalThis 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 experimental entrypoint.
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"types": ["@khulnasoft/workers-types/experimental"]
}
}Importable Types
It's not always possible (or desirable) to modify the tsconfig.json settings for a project to include all the Khulnasoft 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:
import type { Request as WorkerRequest, ExecutionContext } from "@khulnasoft/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.