【问题标题】:Hundreds of JSX element type 'xxx' is not a constructor function for JSX elements?数百个 JSX 元素类型 'xxx' 不是 JSX 元素的构造函数吗?
【发布时间】:2019-09-09 13:32:45
【问题描述】:

我在 React(使用 Redux 和 Antd)、ASP.NET 和 C# 中继承了一个项目,但实际上我遇到了数百个以下类型的错误:

TS2605 (TS) JSX 元素类型“xxx”不是 JSX 元素的构造函数。

给我代码的程序员声称他们没有“有”这些错误(所以他们可能只是忽略它们,因为代码无论如何都会编译)。我认为这可能与我的 React 版本有关(我有最新的)。不同的资源(见下文)建议降级作为解决方法,但这不是我的选择。

我测试了导入 React 的不同方式,但这并没有减少错误的数量。这是我在 Visual Studio(Professional 2017)中看到带红色下划线的代码的一个示例:

import * as React from "react";
// ... 
import { DatePicker } from "antd"; // I also load other things from antd
const { MonthPicker } = DatePicker;
// ... 
<MonthPicker 
     format="MMMM YYYY"
      style={{ width: 150 }}
      placeholder="Date"
      onChange={(e) => { this.handleSelectorChange("Date", e) }}
     value={this.props.selectedDate !== null ? moment(this.props.selectedDate) : null}
/>

换句话说,用户定义的元素xxx 以及MonthPicker 等标准元素都会发生错误。 错误的确切含义是什么,我该如何解决?

有趣的是,尽管出现错误,项目还是运行。

编辑:背景信息

我的package.json 读

^{
  "version": "1.3.3.7",
  "name": "myProject.UI",
  "private": true,
  "scripts": {
    "npm audit": "npm audit",
    "webpack": "webpack -w",
    "webpack-prod": "webpack -p",
    "version": "node --version"
  },
  "dependencies": {
    "antd": "^2.10.0",
    "babel-polyfill": "^6.23.0",
    "download": "^6.2.5",
    "immutable": "^3.8.1",
    "moment": "^2.24.0",
    "react": "15.5.3",
    "react-dom": "15.5.3",
    "react-redux": "^5.0.4",
    "react-router-dom": "^4.1.1",
    "react-router-redux": "^5.0.0-alpha.6",
    "redux": "^3.6.0",
    "redux-saga": "^0.14.8",
    "reselect": "^3.0.0",
    "whatwg-fetch": "^2.0.3"
  },
  "devDependencies": {
    "@types/react": "15.0.21",
    "@types/react-dom": "0.14.23",
    "@types/react-redux": "^4.4.40",
    "@types/react-router-dom": "^4.0.4",
    "@types/react-router-redux": "^5.0.0",
    "awesome-typescript-loader": "3.1.2",
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-plugin-transform-es2015-parameters": "^6.24.1",
    "babel-preset-env": "^1.4.0",
    "css-loader": "^3.2.0",
    "extract-text-webpack-plugin": "^2.1.0",
    "less": "^2.7.2",
    "less-loader": "^4.0.3",
    "source-map-loader": "0.2.1",
    "style-loader": "^0.17.0",
    "ts-loader": "2.0.3",
    "typescript": "2.2.1",
    "webpack": "^2.7.0"
  }
}

我的tsconfig.json 如下所示:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "moduleResolution": "node",
    "jsx": "react",
    "target": "es6"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

资源

2019 年 9 月 16 日编辑:更多资源

【问题讨论】:

  • 听起来像是 VSCode 警告。如果您尝试其他 IDE,您是否会遇到相同的错误?
  • 组件的类型定义是什么?
  • @Ferrybig:我想,没有一个单一的定义,但很多都导致了这个错误。示例:class Component&lt;P, S&gt; implements ComponentLifecycle&lt;P, S&gt; { 中的 myProject.UI\node_modules\@types\react\index.d.t 导致 TS2559 (TS) Type 'Component&lt;P, S&gt;' has no properties in common with type 'ComponentLifecycle&lt;P, S&gt;'.
  • 我在您的package.json 中缺少很多依赖项。例如 TypeScript 本身的版本,但我也没有看到任何 @types 包。也许那些在devDependencies?
  • 最快的方法,删除所有版本并用 * 替换它。这将自动将所有软件包更新到最新版本。 (确保您的代码在更新包时不会中断)

标签: reactjs typescript antd


【解决方案1】:

如果您在以前工作的代码上到处都可以找到它们,那可能是依赖问题。我会尝试通过删除node_modules 并运行yarn install 或npm install(取决于您使用的包管理器)来重新安装所有内容。此外,尝试将您的 react、react-dom、@types/react 和 @types/react-dom 软件包更新到最新版本,例如与

npm install --save @types/react-select -D

【讨论】:

  • 具体来说:代码在调试和生产模式下都运行,虽然这是一个错误,所以我很确定这不是依赖问题。 npm install 没有解决。
  • 另外,您能否发布一个引发这些错误之一的组件声明示例?
  • 我编辑了帖子并包含了package.json 的摘录。虽然它说该项目包含redux-dom,但我还没有看到它被导入。 MonthPicker 是一个 antd 组件,参见ant.design/components/date-picker
  • 我使用您的方法删除了 200 多个错误(连续两次,第一次,什么也没发生)。尽管如此,还是有一些粘性的,特别是一些&lt;Link&gt;-components(来自`react-router-dom')。
  • 很高兴我能帮上忙!很多包,react-router-dom 就是其中之一,都有自己发布的 typedef 包,所以安装 @types/react-router-dom 应该会有所帮助。另请注意,最好将 @types 包添加为开发依赖项,这样它们就不会在您的生产构建中占用不必要的空间,因为 TS 被转换为无法使用它们的常规 JS。只需在安装命令中添加 -D 标志即可。
【解决方案2】:

您可以尝试删除package.lock.json,然后运行npm i。这可能会解决问题。

【讨论】:

  • @sarathantony:是的,我尝试删除 package.lock 并进行更新,但这并没有解决问题。
猜你喜欢
  • 2019-05-29
  • 2019-11-24
  • 2020-05-31
  • 2019-03-06
  • 2019-05-18
  • 2019-07-21
  • 2020-09-16
  • 1970-01-01
  • 2020-08-14
相关资源
最近更新 更多