我试图从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本地服务器,并在执行以下操作后重新启动它:

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

2 -进入SRC文件夹

3 -创建global.d.ts文件

4 -像这样声明模块:

declare module 'module-name';

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

其他回答

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

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

如果您只想为不可用类型的节点模块声明类型,则可以使用这些注释。

解决方案- - - >

在SRC目录中创建一个global.d.ts文件。 因为typescript已经监视SRC目录了 因此,我们避免在tsconfig中显式声明。Json也可以加载特定类型的文件,因为在这样做的时候,我们其他默认的输入位置可能会被覆盖

我在使用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 @types/react-materialize