Typescript react - 找不到模块“react-materialize”的声明文件。 'path/to/module-name.js' 隐含任何类型

新手上路,请多包涵

我正在尝试从 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

原文由 Joy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 866
2 个回答

对于那些想知道我是如何克服这一点的人。我做了一些 hack 的东西。

在我的项目中,我创建了一个名为 @types 的文件夹,并将其添加到 tsconfig.json 以从中查找所有必需的类型。所以它看起来有点像这样 -

 "typeRoots": [
  "../node_modules/@types",
  "../@types"
]

在其中我创建了一个名为 alltypes.d.ts 的文件。从中找出未知的类型。所以对我来说,这些是未知类型,我在那里添加了它。

 declare module 'react-materialize';
declare module 'react-router';
declare module 'flux';

所以现在打字稿不再抱怨找不到类型了。 :) 现在赢得胜利 :)

原文由 Joy 发布,翻译遵循 CC BY-SA 4.0 许可协议

我有一个类似的错误,但对我来说是 react-router 。通过为其安装类型来解决它。

 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.json 并将 noImplicitAny 设置为 false

原文由 Ogglas 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进