如何在 TypeScript 中扩充 process.env? [英] How to augment process.env in TypeScript?

查看:114
本文介绍了如何在 TypeScript 中扩充 process.env?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

process.env 是具有以下定义的 ProcessEnv 类型:

process.env is of type ProcessEnv with this definition:

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

我想扩充这个 TypeScript 接口,使其包含特定于我的应用程序的键,结果类似于:

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;
}

我找不到办法做到这一点,我猜它会是 declare moduledeclare namespace 某处,但找不到具体的方法来实现这一点.

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.

推荐答案

ProcessEnv 必须在 namespace NodeJS 内,不需要声明 [键:字符串]:字符串|undefined;,它继承自初始的ProccessEnv.

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;
  }
}

这篇关于如何在 TypeScript 中扩充 process.env?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆