找不到模块“react/jsx-runtime”的声明文件

Abd*_* Ch 15 javascript typescript reactjs material-ui

我正在使用 Material-ui 与 TypeScript 模板进行反应。我的所有代码都工作正常,但我在多行上收到此错误(不是错误警告,而是在我的代码呈现时出现红线)

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)
Run Code Online (Sandbox Code Playgroud)

Abr*_*ham 32

尝试重启vscode看看是否修复

  • 是的,它解决了这个问题,完全不必要的问题,可能看起来是某种链接器问题。 (2认同)

Jim*_*ode 9

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

declare module "react/jsx-runtime" {
    export default any;
  }
Run Code Online (Sandbox Code Playgroud)

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


小智 6

当您遇到此错误时,通常是因为您安装了库但没有安装该库的类型。

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