我有几个日志语句,如下所示:
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
}
}
关于node.js - 使用 TS 将 process.env 转换为 <any>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51005792/