我试图从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/jsx-runtime”的声明文件

如果有人有这个错误,那么你会惊讶地发现,当使用create-react-app创建react应用程序时,它只在package.json中显示@types/react。但是如果你检查node_modules/@types文件夹,你将找不到任何react文件夹。

解决方案是完全清除node_modules文件夹并重新安装- npm install,或者可能可以使用单个模块安装- npm install @types/react。

其他回答

一个更俗气的方法是加上eg。,在引导。TSX线

import './path/declare_modules.d.ts';

with

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

在declare_modules.d.ts

这是可行的,但在我看来,其他解决方案更好。

比如这个:

{
  "name": "some-package",
  "version": "X.Y.Z",
  "description": "Yada yada yada",
  "main": "./index.js",
  
  "typings": "./index.d.ts",
  
  "repository": "https://github.com/yadayada.git",
  "author": "John Doe",
  "license": "MIT",
  "private": true
}

此外,如果您试图使用的包有自己的类型文件,并且在包中列出了它,则此错误将得到修复。Json类型属性

你可以在这里找到合适的解。你只需要用“反应物质化”代替“反应物品旋转木马”。

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

解决方案- - - >

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

此外,如果您试图使用的包有自己的类型文件,并且在包中列出了它,则此错误将得到修复。Json类型属性

像这样:

{
  "name": "some-package",
  "version": "X.Y.Z",
  "description": "Yada yada yada",
  "main": "./index.js",

  "typings": "./index.d.ts",

  "repository": "https://github.com/yadayada.git",
  "author": "John Doe",
  "license": "MIT",
  "private": true
}