Typescript react - 找不到模块“react-materialize"的声明文件.'path/to/module-name.js' 隐式具有任何类型 [英] Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type
问题描述
我正在尝试从 react-materialize 导入组件 -
I am trying to import components from react-materialize as -
import {Navbar, NavItem} from 'react-materialize';
但是当 webpack 编译我的 .tsx
时,它会抛出上述错误 -
But when the webpack is compiling my .tsx
it throws an error for the above as -
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 一起使用.
Is there any resolution for this? I'm unsure how to resolve this import statement to work with ts-loader
and webpack.
react-materialize 的 index.js
看起来像这样.但是如何在我自己的文件中为模块导入解决这个问题?
The index.js
of react-materialize looks likes this. But how do I resolve this for the module import in my own files?
https://github.com/react-materialize/react-materialize/blob/master/src/index.js
推荐答案
对于那些想知道我是如何克服这个问题的人.我做了一个黑客类的东西.
For those who wanted to know that how did I overcome this . I did a hack kind of stuff .
在我的项目中,我创建了一个名为 @types
的文件夹并将其添加到 tsconfig.json 以从中查找所有必需的类型.所以它看起来有点像这样 -
Inside my project I created a folder called @types
and added it to tsconfig.json for find all required types from it . So it looks somewhat like this -
"typeRoots": [
"../node_modules/@types",
"../@types"
]
在里面我创建了一个名为 alltypes.d.ts
的文件.从中找出未知的类型.所以对我来说这些是未知类型,我在那里添加了它.
And inside that I created a file called alltypes.d.ts
. To find the unknown types from it . so for me these were the unknown types and I added it over there.
declare module 'react-materialize';
declare module 'react-router';
declare module 'flux';
所以现在打字稿不再抱怨找不到类型了.:) 现在双赢 :)
So now the typescript didn't complain about the types not found anymore . :) win win situation now :)
这篇关于Typescript react - 找不到模块“react-materialize"的声明文件.'path/to/module-name.js' 隐式具有任何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!