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

在我的情况下,我有一个react的问题,所以我开始做:

NPM install @types/react

然后是

NPM install @types/react-dom

这对我很有效

如果你在使用npm react时遇到问题,请安装——save @types/reactnpm install——save @types/react

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

e.g.

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

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

确保停止你的react本地服务器,并在执行以下操作后重新启动它:

1-创建。d。Ts文件手动,你只需要做以下工作:

2 -进入SRC文件夹

3 -创建global.d.ts文件

4 -像这样声明模块:

declare module 'module-name';

我之前在这里回答过,很不错