Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 976
Borek Bernard Avatar asked Oct 30 '17 05:10

Borek Bernard


1 Answers

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;
  }
}
like image 128
Michel Alonso Avatar answered Sep 24 '22 22:09

Michel Alonso