数百个JSX元素类型“ xxx”不是JSX元素的构造函数吗?

时间:2019-09-09 13:32:45

标签: reactjs typescript antd

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

  

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

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

我测试了导入React的不同方法,但是并没有减少错误的数量。在以下示例中,我在Visual Studio(专业版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日起编辑:更多资源

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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