typescript 打字稿:找不到模块“react-cards”的声明文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50555859/
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
Typescript: Could not find a declaration file for module 'react-cards'
提问by Ryukote
I am totally new to ReactJS and I found myself stuck in the next thing. I have installed react-cards via npm like this:
我对 ReactJS 完全陌生,我发现自己陷入了下一件事。我已经通过 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. Try 'npm install @types/react-cards' if it exists or add a new declaration(.d.ts) file containing 'declare module 'react-cards';'.
找不到模块“react-cards”的声明文件:“path”隐式具有“any”类型。尝试'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.
我不知道该怎么办。
回答by jantimon
You're importing an npm packages which lacks typeinformation.
您正在导入缺少类型信息的 npm 包。
In your example, TypeScript doesn't understand the properties of Card
when imported. TypeScript is not able to detect a type and refers to any
, which essentially means untyped.
在您的示例中,TypeScript 不了解Card
导入时的属性。TypeScript 无法检测类型并引用any
,这实质上意味着无类型。
For many untyped packages there are @types
npm packages which add those typings for you. You can find them here: microsoft.github.io/TypeSearch
对于许多无类型包,有@types
npm 包可以为您添加这些类型。你可以在这里找到它们:microsoft.github.io/TypeSearch
Unfortunately, there's no @types/react-card
package, but you have options:
不幸的是,没有@types/react-card
包,但你有选择:
You could write the typing information for
react-cards
by yourself and save it into areact-cards.d.ts
file.Disable the warning inside your
tsconfig.json
by setting"noImplicitAny": false
- Reference : https://www.typescriptlang.org/docs/handbook/compiler-options.html
您可以
react-cards
自己编写输入信息并将其保存到react-cards.d.ts
文件中。tsconfig.json
通过设置禁用内部警告"noImplicitAny": false
- 参考:https: //www.typescriptlang.org/docs/handbook/compiler-options.html
Further information: typescript github thread "Importing untyped JS modules"