在 Typescript 中找不到对象名称,但在 Javascript 中找到

发布于 2025-01-16 22:13:16 字数 537 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2025-01-23 22:13:16

考虑将 CONFIG 声明为本地打字稿的全局命名空间。

应该看起来像这样:

import ConfigWebpackPlugin from 'config-webpack';

export default declare global {
  CONFIG: ConfigWebpackPlugin
}

注意:如果您没有全局类型文件,您可以创建一个自定义文件并将其添加到 中的 typeRoots 数组中tsconfig.json 文件。

"typeRoots": ["path/to/custom-type.d.ts" ]

Consider declaring CONFIG as a global namespace for your local typescript.

Should look something like this:

import ConfigWebpackPlugin from 'config-webpack';

export default declare global {
  CONFIG: ConfigWebpackPlugin
}

NOTE: If you don't have a global type file you can make a custom one and add it to the typeRoots array in your tsconfig.json file.

"typeRoots": ["path/to/custom-type.d.ts" ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文