简体   繁体   English

找不到“jquery”的类型定义文件

[英]Cannot find type definition file for 'jquery'

I just ran an update in NuGet to update my my packages.我刚刚在 NuGet 中运行了一个更新来更新我的包。 Afterwards, I can no longer compile and I receive the above error.之后,我无法再编译并收到上述错误。

The jquery definition file is in my project. jquery 定义文件在我的项目中。 But for some reason, the bootstrap definition file can no longer find it.但是由于某种原因,引导程序定义文件再也找不到它了。

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 .我发现这些被称为Triple-Slash Directives But that pages doesn't tell me enough to understand this error.但是那些页面并没有告诉我足以理解这个错误。

It tells me that <reference types="..."/> should be used for @types packages which is what I believe .d.ts files are.它告诉我<reference types="..."/>应该用于@types包,我相信.d.ts文件就是这样。 I tried changing the name of jquery.d.ts to index.d.ts .我尝试将jquery.d.ts的名称jquery.d.tsindex.d.ts And I've tried including the whole path such as ../jquery/jquery.d.ts .我试过包括整个路径,例如../jquery/jquery.d.ts Neither of these worked.这些都没有奏效。

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.我将其复制到 bootstrap 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.首先,我试图链接到定义文件,而不是 TypeScript 文件。 So I should be using reference types , not reference path , according to the above article.因此,根据上述文章,我应该使用reference types ,而不是reference path But reference types does not work.但是reference types不起作用。 What am I missing here?我在这里缺少什么?

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.它们来自 NuGet,看起来是自动生成的。 So why do I need to change this?那么为什么我需要改变这个?

They came from NuGet and look to be automatically generated.它们来自 NuGet,看起来是自动生成的。

Don't use nuget for type definitions.不要将 nuget 用于类型定义。 NPM is the way to go. NPM 是要走的路。

npm install @types/jquery -D 

And then you can:然后你可以:

<reference types="jquery"/>

welcome to the future 🌹欢迎来到未来🌹

我发现在安装@types/jquery 后,在 jquery.slim.d.ts /// <reference path="../JQuery.d.ts" /> /// <reference types="jquery" />更改为/// <reference path="../JQuery.d.ts" />为我解决了这个问题。

One option will be setting the property typeRoots on 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 typings folder inside the Scripts folder.一种选择将是设置你的tsconfig.json财产typeRoots(你需要创建一个,如果你的项目还没有的话),以包含定义文件的文件夹的实际路径,如果你正在使用的NuGet和FreedomTyped 应该是Scripts文件夹中的Typings文件夹。 Therefore:所以:

{
  "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 不是一种选择。

Wanted to give an updated answer when getting this error as part of an Angular project.当作为 Angular 项目的一部分收到此错误时,想要提供更新的答案。 Was getting the error when running unit tests, and my problem was that in my tsconfig.json I had jQuery type listed as:运行单元测试时出现错误,我的问题是在我的tsconfig.json我将 jQuery 类型列为:

{
  "compilerOptions": {
    ...,
    "types": [
      "node, jQuery"
    ]
  }
}

The fix for me was to use all lower case:我的解决方法是使用所有小写:

{
  "compilerOptions": {
    ...,
    "types": [
      "node, jquery"
    ]
  }
}

如果Web项目正在使用libman ,请右键单击libman.json并单击“恢复客户端库”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM