我试图从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应用中 会有一个react-app-env.d。SRC文件夹下的Ts文件 只要在这里声明模块

/// <reference types="react-scripts" />
declare module 'react-color/lib/components/alpha/AlphaPointer'

其他回答

我费了好大劲才拿到的 在react应用中 会有一个react-app-env.d。SRC文件夹下的Ts文件 只要在这里声明模块

/// <reference types="react-scripts" />
declare module 'react-color/lib/components/alpha/AlphaPointer'

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

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

比如这个:

{
  "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类型属性

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

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

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

声明文件发布

我可以如何贡献