Use `WorkerGlobalScope` inside `global` for binding.
The suggested way of accessing env is to create a binding.d.ts file with the following content.
declare global {
const ENV_KEY: string
}
The problem with this approach is if the env variable is not declared the script just throws at runtime.
A better way would be to extent WorkerGlobalScope like below.
declare global {
export interface WorkerGlobalScope {
ENV_KEY: string
}
}
This way the the env vars are accessed via the self object, thereby have flexibility for error handling and defaults.
This is a great sugestion . i have been batling with envs in my project for two days now just noticed that bindings file is not working.
Hey! 👋 Thank you for the suggestion. I don't think we'll go ahead with it though, we'd still uses of bindings in the global scope without a self. prefix to type check. Just putting types on WorkerGlobalScope will prevent this.