【问题标题】:Could not find a declaration file for module 'react/jsx-runtime'找不到模块“react/jsx-runtime”的声明文件
【发布时间】:2021-04-12 06:01:46
【问题描述】:

我正在使用 material-ui 与打字稿模板做出反应。我的所有代码都运行良好,但我在多行上收到此错误(不是错误警告,而是在我的代码呈现时出现红线)

Could not find a declaration file for module 'react/jsx-runtime'. 'C:/Users/dell/Desktop/Web current Projects/typescript/card/node_modules/react/jsx-runtime.js' implicitly has an 'any' type.
  If the 'react' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react`ts(7016)

【问题讨论】:

标签: javascript reactjs typescript material-ui


【解决方案1】:

根据@NearHuscarl 的评论,快速解决方法是为react/jsx-runtime 创建自己的声明模块。您可以通过打开项目根目录中的 react-app-env.d.ts 文件(使用 create-react-app 时默认创建)来执行此操作。然后在其中添加以下脚本:

declare module "react/jsx-runtime" {
    export default any;
  }

create-react-app 团队不认为这是 create-react-app 需要解决的问题,并建议这是 typescript 版本 4 的问题。因此,您也可以降级 typescript 版本,直到 typescript 团队提供后续版本中的修复。

【讨论】:

    【解决方案2】:

    当你出现这个错误时,通常是因为你安装了一个库,而你没有安装那个库的类型。

    要修复此错误,您需要使用包管理器(npm 或 yarn)安装 @types/library-name

    【讨论】:

      猜你喜欢
      • 2019-03-16
      • 2021-04-30
      • 2019-07-26
      • 2021-02-21
      • 2017-05-29
      • 1970-01-01
      • 2022-06-12
      • 2022-11-06
      • 2021-01-18
      相关资源
      最近更新 更多