在 Typescript 中找不到对象名称,但在 Javascript 中找到
我正在使用 config-webpack 来管理应用程序中的配置设置。 https://www.npmjs.com/package/config-webpack
这需要所有JSON 文件中的配置,并使它们作为 JS 对象在应用程序中可用 - 即 CONFIG.SOMEBOOLEANSETTNIG
在我的项目中,如果我有一个 .js 文件,则可以正常工作,并且以下语句可以解决;
if (CONFIG.SOMEBOOLEANSETTNIG==true) { ... }
但是,.ts 或 .tsx 文件中的相同代码会显示错误,其中 CONFIG 下划线为“找不到名称 'CONFIG' ts” (2304)”并且编译失败。
我觉得我错过了一些非常简单的东西,但我是 TS 的新手!谢谢。
我尝试查看 Typescript 语法文档,但我看不出哪里出错了,所以任何帮助将不胜感激。
I am using config-webpack to manage configuration settings in my application. https://www.npmjs.com/package/config-webpack
This takes all the configs in a JSON file and makes them available in the application as a JS object - i.e. CONFIG.SOMEBOOLEANSETTNIG
In my project, if I have a .js file this works fine and the following statement resolves;
if (CONFIG.SOMEBOOLEANSETTNIG==true) { ... }
However, the same code in a .ts or .tsx file displays an error with the CONFIG underlined with "Cannot find name 'CONFIG' ts(2304)" and the compilation fails.
I feel that I am missing something really simple but I am new to TS! Thanks.
I've tried looking into Typescript syntax documentation but I can't see where I am going wrong, so any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑将
CONFIG
声明为本地打字稿的全局命名空间。应该看起来像这样:
注意:如果您没有全局类型文件,您可以创建一个自定义文件并将其添加到
中的
typeRoots
数组中tsconfig.json 文件。Consider declaring
CONFIG
as a global namespace for your local typescript.Should look something like this:
NOTE: If you don't have a global type file you can make a custom one and add it to the
typeRoots
array in yourtsconfig.json
file.