@timsexperiments/ts-plugin-workers v0.0.0
@timsexperiments/ts-plugin-workers
A TypeScript server plugin for automatically creating your Env
type in workers based off of your wrangler.toml
configuration. This plugin simplifies the process of configuring environment types in Cloudflare Workers by automatically generating the Env
type declaration from your project's wrangler.toml
file.
Installation
You can install @timsexperiments/ts-plugin-workers
using your preferred JavaScript package manager. Run one of the following commands based on the package manager you are using:
# For Bun
bun add -D @timsexperiments/ts-plugin-workers
# For npm
npm install -D @timsexperiments/ts-plugin-workers
# For pnpm
pnpm add -D @timsexperiments/ts-plugin-workers
Usage
To use the plugin, add it to your tsconfig.json within your Cloudflare Workers project as shown below:
{
"compilerOptions": {
// ... Other configuration values
"plugins": [{ "name": "@timsexperiments/ts-plugin-workers" }]
}
}
This project will read your workers configuration from wrangler.toml or the file specified in the config option of your plugin configuration in tsconfig.json. It outputs the types to a global type called Env in a bindings.d.ts file or whatever is specified in the out option of your plugin configuration in tsconfig.json.
Extending the Env Type
Once you have configured your project, in the standard boilerplate for your Cloudflare Worker project, you can extend your Env type by wrapping the existing Env type inside a declare global. This allows you to merge any types you define with those generated by the plugin.
declare global {
interface Env {
// Example binding to various Cloudflare Worker APIs
OVERRIDDEN_ENV_VAR: string;
}
}
Contributing
Please see the CONTRIBUTING.md file for detailed instructions on how to contribute to this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project Status
The project is currently in beta. It works for specific use cases but might not fit all. If you encounter issues or have suggestions for improvement, please open an issue on GitHub.
Technical Details
For VS Code users, you will need to select to use the workspace version of TypeScript for both usage and contribution, especially for debugging. To select the TypeScript version in vs code open the command palette, select "TypeScript: Select TypeScript Version...", and choose "Use Workspace Version".
Examples
Check out the example/ folder within the repository for a sample project demonstrating how to use the plugin.
1 year ago