Search code examples
node.jstypescripttypescript-typings

How to augment process.env in TypeScript?


process.env is of type ProcessEnv with this definition:

export interface ProcessEnv {
    [key: string]: string | undefined;
}

I'd like to augment this TypeScript interface so that it contains the keys specific to my app, so that the result is something like:

export interface ProcessEnv {
    MY_VARIABLE_1: string;
    MY_OTHER_VARIABLE: string;
    [key: string]: string | undefined;
}

I cannot find a way to do it, I guess it will be declare module or declare namespace somewhere but cannot find a specific way to achieve this.


Solution

  • The ProcessEnv must be inside the namespace NodeJS and doesn't need to declare the [key: string]: string | undefined;, it inherits from the initial ProccessEnv.

    declare namespace NodeJS {
      export interface ProcessEnv {
        MY_VARIABLE_1: string;
        MY_OTHER_VARIABLE: string;
      }
    }