typescript 找不到“jquery”的类型定义文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41989621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find type definition file for 'jquery'
提问by embedded.kyle
I just ran an update in NuGet to update my my packages. Afterwards, I can no longer compile and I receive the above error.
我刚刚在 NuGet 中运行了一个更新来更新我的包。之后,我无法再编译并收到上述错误。
The jquery definition file is in my project. But for some reason, the bootstrap definition file can no longer find it.
jquery 定义文件在我的项目中。但是由于某种原因,引导程序定义文件再也找不到它了。
It's been a while since I worked on this project. I'm pretty sure I had my definition files in a completely different directory before the update. But most of the code and file structure has been created automatically by Visual Studio NuGet packages.
我从事这个项目已经有一段时间了。我很确定在更新之前我的定义文件在一个完全不同的目录中。但是大部分代码和文件结构是由 Visual Studio NuGet 包自动创建的。
Where are these links or references located so that the definition files can be found properly? I'm very new to TypeScript, so if you can explain in detail or provide links for further reading, it would be appreciated
这些链接或引用位于何处以便可以正确找到定义文件?我对 TypeScript 很陌生,所以如果你能详细解释或提供进一步阅读的链接,将不胜感激
Edit:
编辑:
I've discovered that these are called Triple-Slash Directives. But that pages doesn't tell me enough to understand this error.
我发现这些被称为Triple-Slash Directives。但是那些页面并没有告诉我足以理解这个错误。
It tells me that <reference types="..."/>
should be used for @types
packages which is what I believe .d.ts
files are. I tried changing the name of jquery.d.ts
to index.d.ts
. And I've tried including the whole path such as ../jquery/jquery.d.ts
. Neither of these worked.
它告诉我<reference types="..."/>
应该用于@types
我认为.d.ts
文件所在的包。我尝试将名称更改jquery.d.ts
为index.d.ts
。我试过包括整个路径,例如../jquery/jquery.d.ts
. 这些都没有奏效。
Finally, I found the following line in angular.d.ts
:
最后,我在以下行中找到了angular.d.ts
:
/// <reference path="../jquery/jquery.d.ts" />
I copied that into the bootstrap index.d.ts
and that seemed to fix the error.
我将它复制到引导程序中index.d.ts
,这似乎解决了错误。
But this doesn't make sense to me. First, I'm trying to link to a definition file, not a TypeScript file. So I should be using reference types
, not reference path
, according to the above article. But reference types
does not work. What am I missing here?
但这对我来说没有意义。首先,我试图链接到定义文件,而不是 TypeScript 文件。因此,根据上述文章,我应该使用reference types
,而不是reference path
。但reference types
不起作用。我在这里错过了什么?
The article also says:
文章还说:
Use these directives only when you're authoring a d.ts file by hand.
仅当您手动创作 d.ts 文件时才使用这些指令。
I didn't write these files. They came from NuGet and look to be automatically generated. So why do I need to change this?
我没有写这些文件。它们来自 NuGet,看起来是自动生成的。那么为什么我需要改变这个?
回答by basarat
They came from NuGet and look to be automatically generated.
它们来自 NuGet,看起来是自动生成的。
Don't use nuget for type definitions. NPM is the way to go.
不要将 nuget 用于类型定义。NPM 是必经之路。
npm install @types/jquery -D
And then you can:
然后你可以:
<reference types="jquery"/>
welcome to the future
欢迎来到未来
回答by Z Wu
I found that changing /// <reference types="jquery" />
to /// <reference path="../JQuery.d.ts" />
in jquery.slim.d.ts after installing @types/jquery fixed this issue for me.
我发现在安装 @types/jquery 后更改/// <reference types="jquery" />
为/// <reference path="../JQuery.d.ts" />
jquery.slim.d.ts 为我解决了这个问题。
回答by VictorArcas
One option will be setting the property typeRootson your tsconfig.json(you will need to create one if your project still don't have it) to the actual path of the folder that contains the definition files, that if you are using Nuget and DefinitelyTyped it should be the typingsfolder inside the Scriptsfolder. Therefore:
其中一个选项将设置该属性typeRoots您tsconfig.json(你需要创建一个,如果你的项目还没有的话),以包含定义文件的文件夹的实际路径,如果你正在使用的NuGet和FreedomTyped 应该是Scripts文件夹中的Typings文件夹。所以:
{
"compilerOptions": {
"typeRoots": [
"./Scripts/typings"
]
}
}
What basarat posted is the correct 2020 way to proceed, but maybe you are working on a legacy project where migrating to NPM is not an option right now.
basarat 发布的是 2020 年正确的继续方式,但也许您正在处理一个遗留项目,现在迁移到 NPM 不是一种选择。