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


当前回答

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

其他回答

react-router-dom有同样的错误。

此错误发生在您正在处理typescript项目时,默认模块没有提供typescript所需的类型。

所以我在npmjs.com上搜索了一个名为@types/react-router-dom的包,如果它不是你正在寻找的react-router-dom,然后用你的untyped模块替换react-router-dom。

在npm网站上寻找最后一个版本,然后将它添加到你的包中。Json是这样的:

[...]
"dependencies": {
    [...]
    "@types/react-router-dom": "^5.1.8",
    [...]
  },
[...]

一旦完成,运行yarn install

那么它应该会摇滚起来!

我也有同样的问题,但不一定与typescript有关,所以我在这些不同的选项上有点挣扎。我正在使用特定的模块react-portal-tooltip创建一个非常基本的create-react-app,得到类似的错误:

无法找到模块“react-portal-tooltip”的声明文件。'/node_modules/react-portal-tooltip/lib/index.js'隐含有一个'any'类型。 尝试npm install @types/react-portal-tooltip,如果它存在,或者添加一个包含声明模块'react-portal-tooltip'的新声明(.d.ts)文件;ts(7016)

我先尝试了很多步骤——添加各种各样的。d。Ts文件,各种NPM安装。

但最终对我有用的是触摸src/declare_modules.d.ts 然后在src/declare_modules.d.ts中:

declare module "react-portal-tooltip";

在src/App.js中:

import ToolTip from 'react-portal-tooltip';
// import './declare_modules.d.ts'

我在不同的位置使用这种通用的“声明模块”策略(我是一个初学者),所以我认为这将适用于不同的选项,但我包括了对我有效的路径。

我最初认为导入'./declare_modules.d。Ts '是必要的。虽然现在看起来并不是这样!但我把这一步也算进去了以防它能帮到别人。

这是我的第一个stackoverflow回答,所以我在这里为分散的过程道歉,希望它仍然有帮助!:)

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

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

我在使用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"]
}