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


当前回答

如果你正在使用的模块没有@types/<package>,你可以通过在上面添加// @ts-ignore注释来轻松规避这个问题。

// @ts-ignore
import { Navbar, NavItem } from 'react-materialize';

或者你也可以创建下面缺少的@types/<package>:

声明文件发布

我可以如何贡献

其他回答

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

npm install --save @types/react-redux

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

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

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

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

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

现在typescript不再抱怨找不到类型了。现在是双赢的局面:)

我在使用react-redux类型时也遇到了同样的问题。最简单的解决方案 被添加到tsconfig.json:

"noImplicitAny": false

例子:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["es6"],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "target": "esnext",
    "noImplicitAny": false,
  },
  "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
}

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

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