Using environment variables in Typescript

Viewed 996

Is there a proper way to include environment variables in a .ts file? They are declared in wrangler.toml or through the CLI, but Typescript won't know they are there.

Currently I either use a .js file to declare these vars and then import into a .ts

//env.js
const SOMEVAR = SOMEVAR

Or I will need to use a @ts-ignore comment.

I've tried process.env but as expected this fails as the script isn't run in Node.

1 Answers

You can inform TypeScript of your global variables by using the declare global { ... } syntax in your script:

declare global {
  const SOMEVAR: string
  const ANOTHERVAR: string
}

// now you can use SOMEVAR and ANOTHERVAR as global vars
Related