我试图从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


当前回答

工作很好

npm install @types/react-materialize

其他回答

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

我有一个类似的错误,但对我来说,这是反应路由器。通过为它安装类型来解决它。

npm install --save @types/react-router

错误:

(6,30): error TS7016: Could not find a declaration file for module 'react-router'. '\node_modules\react-router\index.js' implicitly has an 'any' type.

如果你想在站点范围内禁用它,你可以编辑tsconfig。将noImplicitAny设置为false。

可能在某些情况下,某些依赖项没有类型,ts会强制您为此包含类型。有一种变通方法可以解决这个问题,那就是使用javascript。

e.g.

import {Charts} from "react-charts";//error:无法找到模块'react-charts'的声明文件。

const ReactChart = require("react-charts");/ /没有错误

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

我也遇到过同样的问题,最简单的解决办法就是

NPM I—save-dev @types/react-router-dom