node.js - 使用 TS 将 process.env 转换为 <any>

标签 node.js typescript typescript2.0 tsc

我有几个日志语句,如下所示:

log.info('docker.r2g run routine is waiting for exit signal from the user. The container id is:', chalk.bold(process.env.r2g_container_id));
log.info('to inspect the container, use:', chalk.bold(`docker exec -it ${process.env.r2g_container_id} /bin/bash`));
log.info('to stop/kill the container, use kill, not stop:', chalk.bold(`docker kill ${process.env.r2g_container_id}`));

当我使用 tsc 对其进行转译时,出现以下错误:

src/commands/run/run.ts(132,94): error TS2339: Property 'r2g_container_id' does not exist on type 'ProcessEnv'.

133 log.info('to stop/kill the container, use kill, not stop:', chalk.bold(`docker kill ${process.env.r2g_container_id}`));

process.env强制转换为any或其他什么的最好方法是什么,以消除这些错误?或者也许我可以扩展 ProcessEnv 以包含我正在寻找的环境变量?前者看起来不错。

我尝试过这个:

declare global {

  namespace NodeJS {
    export interface ProcessEnv {
      r2g_container_id: string,
      docker_r2g_is_debug: string
    }
  }

}

但这并不完全正确。

这是一个类似的问题,我们可以引用:using process.env in TypeScript

最佳答案

这似乎也有效:

declare namespace NodeJS {
  export interface EnvironmentVariables {
    r2g_container_id: string,
    docker_r2g_is_debug: string
  }
}

在这里找到: https://github.com/typings/registry/issues/770

关于node.js - 使用 TS 将 process.env 转换为 <any>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51005792/

相关文章:

node.js - 可视化代码和 node_modules 中的 typescript

TypeScript 删除扩展重复项

typescript - 缩小捕获中的错误类型

javascript - 如何使用 typescript 修改对象数组(原始形式到新形式)

typescript - 如何克隆TypeScript对象数组?

typescript - 具有多个类型参数的泛型中的推断类型

javascript - npm 无法安装 scrypt。 Node gyp 重建

node.js - 错误 : listen EACCES 0. 0.0.0:443

javascript - 从 .txt 文件读取并显示数据的 React 组件

node.js - npm启动后返回Cmder提示符