sajad torkamani

Create a .d.ts file somewhere (e.g., src/types/env.d.ts) and make sure this is included in the typeRoots setting in your tsconfig.json.

Example typeRoots value in tsconfig.json:

 "compilerOptions: {
   "typeRoots": [
     "./node_modules/@types",
     "./src/types"
   ]
}

Then extend the NodeJS.ProcessEnv interface in your .d.ts file:

declare module 'process' {
  declare global {
    namespace NodeJS {
      interface ProcessEnv {
        S3_ACCESS_KEY: string
        S3_SECRET_KEY: string
      }
    }
  }
}

Now, you can write something like process.env.FOO without any TypeScript errors.

Tagged: TypeScript

Leave a comment

Your email address will not be published. Required fields are marked *