Extend process.env in TypeScript
12 May 2022 (Updated 17 May 2022)
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
:
"typeRoots": [
"./node_modules/@types",
"./src/types"
],
Then extend the NodeJS.ProcessEnv
interface in your .d.ts
file:
declare namespace NodeJS {
export interface ProcessEnv {
FOO: string
BAR: string
}
}
Now, you can write something like process.env.FOO
without any TypeScript errors.
Tagged:
TypeScript
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment