Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environment variables in Typescript

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.

like image 502
David Min Avatar asked Jun 30 '26 20:06

David Min


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
like image 83
Luke Carr Avatar answered Jul 04 '26 15:07

Luke Carr