【发布时间】: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"
]
}
资源
- JSX element type 'Element[]' is not a constructor function for JSX elements
- JSX element type 'xxx' is not a constructor function for JSX elements. Property 'yyy' is protected in type 'xxx' but public in type 'ElementClass'
- https://github.com/Microsoft/TypeScript-React-Starter/issues/45
2019 年 9 月 16 日编辑:更多资源
【问题讨论】:
-
听起来像是 VSCode 警告。如果您尝试其他 IDE,您是否会遇到相同的错误?
-
组件的类型定义是什么?
-
@Ferrybig:我想,没有一个单一的定义,但很多都导致了这个错误。示例:
class Component<P, S> implements ComponentLifecycle<P, S> {中的myProject.UI\node_modules\@types\react\index.d.t导致TS2559 (TS) Type 'Component<P, S>' has no properties in common with type 'ComponentLifecycle<P, S>'. -
我在您的
package.json中缺少很多依赖项。例如 TypeScript 本身的版本,但我也没有看到任何@types包。也许那些在devDependencies? -
最快的方法,删除所有版本并用 * 替换它。这将自动将所有软件包更新到最新版本。 (确保您的代码在更新包时不会中断)
标签: reactjs typescript antd