简体   繁体   English

打字稿:找不到模块“react-cards”的声明文件

[英]Typescript: Could not find a declaration file for module 'react-cards'

I am totally new to ReactJS and I found myself stuck in the next thing.我对 ReactJS 完全陌生,我发现自己陷入了下一件事。 I have installed react-cards via npm like this:我已经通过 npm 安装了 react-cards,如下所示:

npm install --save react-cards

Installation was ok and I want to import it in my code like this:安装没问题,我想像这样在我的代码中导入它:

import Card from 'react-cards';

But then I got error saying this:但是后来我说这个错误:

Could not find a declaration file for module 'react-cards':'path' implicitly has an 'any' type.找不到模块“react-cards”的声明文件:“path”隐式具有“any”类型。 Try 'npm install @types/react-cards' if it exists or add a new declaration(.d.ts) file containing 'declare module 'react-cards';'.尝试'npm install @types/react-cards'(如果存在)或添加一个包含'declare module'react-cards';'的新声明(.d.ts)文件。

I have tried with npm install @types/react-cards but nothing changed.我试过 npm install @types/react-cards 但没有任何改变。

I don't know what to do.我不知道该怎么办。

You're importing an npm packages which lacks type information.您正在导入缺少类型信息的 npm 包。

In your example, TypeScript doesn't understand the properties of Card when imported.在您的示例中,TypeScript 在导入时不了解Card的属性。 TypeScript is not able to detect a type and refers to any , which essentially means untyped. TypeScript 无法检测类型并引用any ,这实质上意味着无类型。

For many untyped packages there are @types npm packages which add those typings for you.对于许多无类型包,有@types npm 包可以为您添加这些类型。 You can find them here: microsoft.github.io/TypeSearch你可以在这里找到它们: microsoft.github.io/TypeSearch

Unfortunately, there's no @types/react-card package, but you have options:不幸的是,没有@types/react-card包,但你有选择:

  1. You could write the typing information for react-cards by yourself and save it into a react-cards.d.ts file.您可以自己编写react-cards的输入信息并将其保存到react-cards.d.ts文件中。

  2. Disable the warning inside your tsconfig.json by setting "noImplicitAny": false - Reference : https://www.typescriptlang.org/docs/handbook/compiler-options.html通过设置"noImplicitAny": false禁用tsconfig.json的警告"noImplicitAny": false - 参考: https "noImplicitAny": false

Further information: typescript github thread "Importing untyped JS modules"更多信息: typescript github 线程“导入无类型 JS 模块”

If you're using VS Code, there's a quick fix suggestion for this you can invoke with Ctrl + .如果您使用 VS Code,有一个快速修复建议,您可以使用Ctrl + 调用.

安装缺少的类型

You can also automatically add all missing types with typesync like this:您还可以使用typesync自动添加所有缺少的类型,如下所示:

npx typesync

If the package doesn't have it's own types, and it hasn't been added to DefinitelyTyped , you'll need to add a new definitions file如果包没有自己的类型,并且还没有被添加到DefineTyped ,你需要添加一个新的定义文件

Further Reading进一步阅读

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

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