我试图从react-materialize导入组件

import {Navbar, NavItem} from 'react-materialize';

但是当webpack编译我的.tsx时,它会为上面的-抛出一个错误

ERROR in ./src/common/navbar.tsx
(3,31): error TS7016: Could not find a declaration file for module 'react-materi
alize'. 'D:\Private\Works\Typescript\QuickReact\node_modules\react-materialize\l
ib\index.js' implicitly has an 'any' type.

有什么解决办法吗?我不确定如何解析此导入语句以使用ts-loader和webpack。

react-materialize的index.js是这样的。但是我如何解决这个模块导入在我自己的文件?

https://github.com/react-materialize/react-materialize/blob/master/src/index.js


当前回答

react-router-dom有同样的错误。

此错误发生在您正在处理typescript项目时,默认模块没有提供typescript所需的类型。

所以我在npmjs.com上搜索了一个名为@types/react-router-dom的包,如果它不是你正在寻找的react-router-dom,然后用你的untyped模块替换react-router-dom。

在npm网站上寻找最后一个版本,然后将它添加到你的包中。Json是这样的:

[...]
"dependencies": {
    [...]
    "@types/react-router-dom": "^5.1.8",
    [...]
  },
[...]

一旦完成,运行yarn install

那么它应该会摇滚起来!

其他回答

对于我的情况,问题是类型没有被添加到包。为了修复它,我运行了npm install——save-dev @types/react-redux,注意——save-dev

您所需要做的就是运行下面的脚本。然后,移除/重新安装要使用的模块。

npm install --save @types/react-redux

尝试添加到tsconfig。"noImplicitAny": false 为我工作

当我使用typescript添加react-router-dom到新的CRA应用程序时,我有这个问题。在我添加@types/react-router后,这个问题得到了修复。

只要为react安装必要的类型,它就应该解决这个错误。

如果你用的是纱线:

yarn add @types/react @types/react-dom @types/react-router-dom -D

如果你正在使用npm:

npm install @types/react @types/react-dom @types/react-router-dom --save-dev