gpt4 book ai didi

angularjs - 不一致的 "Cannot find name ' x'"VS 代码中的 Typescript 错误

转载 作者:太空狗 更新时间:2023-10-29 17:18:59 26 4
gpt4 key购买 nike

我的应用编译(转译)得很好,但 Visual Studio Code 仍然显示很多错误:

enter image description here

在某些情况下(例如 angularionic),问题是无法识别通过包含 Angular/Ionic 添加的全局变量/命名空间.大多数错误的形式为“找不到名称‘angular/ionic/ng’(等等)”。

更奇怪的是,我注意到加载 VS Code 时最初打开的文件根本没有任何错误。红色下划线错误在其他选项卡/编辑器的其他文件中。

这是怎么回事?我如何让 VS Code 始终确认这些全局变量/命名空间确实存在?

最佳答案

在追逐这个问题的许多悲伤日子之后 - 我终于找到了 GitHub Issue在 VS Code GitHub 上解释了正在发生的事情。

tl;dr

我的 tsconfig.json 文件配置不正确。为了修复它,我删除了 files 部分。您可能还需要在您的项目中删除它,或者只是“修复”它以包含所有相关的 .ts 文件。

更长的版本

Adding a files [section] limits our project to those two files and if you open other files not referenced by those two files then they end up in an isolated virtual project. You either need to leave out the files section (then all .ts files underneath the tsconfig.json file are automatically considered part of the project) or you need to list all files of your project in that section.

我原来的 `tsconfig.json 文件是:

{
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"removeComments": true,
"noImplicitAny": true
},
"files": [
"typings/index.d.ts",
"src/typings/index.d.ts"
]
}

因此,VS Code 认为我的项目只包含两个文件。我加载的其他 .ts 文件被认为是“孤立的虚拟项目”——不难理解它们产生错误的原因。

我将我的 tsconfig.json 文件更改为以下内容:

{
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"removeComments": true,
"noImplicitAny": true
}
}

问题解决了!

关于angularjs - 不一致的 "Cannot find name ' x'"VS 代码中的 Typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com