Typescript react - 找不到module“react-materialize”的声明文件。'path/to/module-name.js' 隐式具有任何类型

IT技术 reactjs webpack webpack-2
2021-03-30 04:47:30

我正在尝试从 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-loaderwebpack 一起使用。

index.jsreact-Materialise的长相很喜欢这一点。但是如何在我自己的文件中为module导入解决这个问题?

https://github.com/react-materialize/react-materialize/blob/master/src/index.js

6个回答

我有一个类似的错误,但对我来说是react-router. 通过为它安装类型来解决它。

npm install --save @types/react-router

错误:

(6,30): error TS7016: Could not find a declaration file for module 'react-router'. '\node_modules\react-router\index.js' implicitly has an 'any' type.

如果您想在站点范围内禁用它,您可以改为编辑tsconfig.json并设置noImplicitAnyfalse.

yarn add @types/react-router-dom 使用 create-react-app 时为我修复了它
2021-05-26 04:47:30
虽然目前还没有这样的@types/react-navigation-tabs 包,但提到的"noImplicitAny": false,仍然禁用了它(我得到这个问题是因为使用 react-native init MyProject --template typescript,我认为至少是这样)👍
2021-05-29 04:47:30
yarn add @types/react-router-dom也为我修好了!:) 感谢分享 thenomadicmann
2021-05-31 04:47:30
或者 yarn add @types/react-router --dev
2021-06-04 04:47:30
最佳答案。react-slick我的项目相同的问题是 TypeScript 项目。使固定npm i --save-dev @types/react-slick
2021-06-07 04:47:30

对于那些想知道我是如何克服这个问题的人。我做了一个黑客类的东西。

在我的项目中,我创建了一个名为的文件夹@types并将其添加到 tsconfig.json 以从中查找所有必需的类型。所以它看起来有点像这样 -

"typeRoots": [
  "../node_modules/@types",
  "../@types"
]

在里面我创建了一个名为alltypes.d.ts. 从中找出未知的类型。所以对我来说,这些是未知类型,我在那里添加了它。

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

所以现在typescript不再抱怨找不到类型了。:) 现在双赢局面 :)

这工作得很好,但在require 用于图像等其他内容时会导致问题基本上,它只是不允许你使用require任何东西。
2021-06-04 04:47:30
我在react-countdown-clock创建@types文件夹时遇到问题并执行了上述所有步骤。仍然得到同样的错误。
2021-06-09 04:47:30
此解决方案不再适用于最新的 TS。 nvalid module name in augmentation. Module 'some-dumb-lib-with-no-types' resolves to an untyped module at '/path/to/module', which cannot be augmented.
2021-06-16 04:47:30
还有一点值得一提:根据源的位置,您可能还需要将“../types”添加到“includes”数组中
2021-06-17 04:47:30
我错过了关于它在单独文件中的部分。似乎将这些declare语句放在一个名为index.d.tsfix it的文件中我不必接触 typeRoots。
2021-06-20 04:47:30

我对 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"]
}
我认为这是一个黑客,虽然这解决了问题,但是这个问题有没有有效和永久的解决方案?
2021-05-27 04:47:30
这在IntelliJIDE 中效果很好,但是自定义typeRoots方法(这是公认的答案)甚至在VS CodeIDE 中也有效
2021-05-29 04:47:30
我认为这是这个问题的最佳解决方案,我认为 npm i 每个抛出错误的包都不可行
2021-06-10 04:47:30
这是一个非常糟糕的做法,因为该any类型只是忽略了 Typescript linting。那么有什么意义呢?这是向普通 javascript 的后退一步,而不是使用 Typescript。对我来说,以下答案解决了它,并且被认为要好得多:stackoverflow.com/questions/41462729/...
2021-06-11 04:47:30
使用typescript在 VSC 上完美工作并做出反应
2021-06-20 04:47:30

如果@types/<package>您正在使用的module没有,您可以通过在// @ts-ignore上面添加评论来轻松规避该问题,即

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

或者,您可以创建缺少的@types/<package>以下内容:

申报文件发布

绝对类型我如何贡献

这个包还没有@types可用的 BTW。在 repo 上创建了一个问题... github.com/geelen/react-snapshot/issues/131
2021-06-16 04:47:30

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

1-.d.ts手动创建 文件,您只需要执行以下操作:

2 - 输入 src folder

3 - 创建global.d.ts文件

4 - 在其中声明module,如:

declare module 'module-name';

之前在这里回答过,很好

谢谢!这是这里提到的所有解决方案中效果最好的
2021-05-28 04:47:30
如果在执行上述操作后仍然无法正常工作,请确保停止您的本地反应服务器并重新启动它。 npm start
2021-06-17 04:47:30