全局范围的增强只能直接嵌套在外部模块或环境模块声明中 (2669) [英] Augmentations for the global scope can only be directly nested in external modules or ambient module declarations(2669)

查看:36
本文介绍了全局范围的增强只能直接嵌套在外部模块或环境模块声明中 (2669)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的 NodeJS 配置存储在全局范围内.

我试图遵循这个 => 在 node.js 中扩展 TypeScript 全局对象 和 stackoverflow 上的其他解决方案,

我创建了一个名为 global.d.ts 的文件,其中包含以下代码

声明全局{命名空间 NodeJS {接口全局{配置:MyConfigType}}}

<块引用>

全局作用域的增强只能直接嵌套在外部模块或环境模块声明.ts(2669)

但这样做效果很好 =>

声明模块 NodeJS {接口全局{配置:MyConfigType}}

问题是,我需要导入文件 MyConfigType 来输入配置,但第二个选项不允许这样做.

解决方案

你可以像这样表明文件是一个模块:

export {};声明全局{命名空间 NodeJS {接口全局{配置:MyConfigType}}}

I would like to store my NodeJS config in the global scope.

I tried to follow this => Extending TypeScript Global object in node.js and other solution on stackoverflow,

I made a file called global.d.ts where I have the following code

declare global {
    namespace NodeJS {
      interface Global {
          config: MyConfigType
      }
    }
  }

Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.ts(2669)

but doing this works fine =>

declare module NodeJS  {
    interface Global {
        config: MyConfigType
    }
}

the problem is, I need to import the file MyConfigType to type the config, but the second option do not allow that.

解决方案

You can indicate that the file is a module like so:

export {};

declare global {
    namespace NodeJS {
        interface Global {
            config: MyConfigType
        }
    }
}

这篇关于全局范围的增强只能直接嵌套在外部模块或环境模块声明中 (2669)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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