【发布时间】:2019-09-28 21:54:34
【问题描述】:
即使它正常工作,Visual Studio Code 仍会继续显示错误,如下面的屏幕截图所示。 AFAIK,我的 ESLint 似乎不了解 React.Fragment。它会向我拥有的任何<React.Fragment> 或<Fragment> 显示错误。
如何禁用或修复此问题?
这是一个电子反应项目,带有打字稿。我从
开始这个项目electron-forge init my-new-project --template=react-typescript
然后我将react、@types/react、@types/react-dom 和react-dom 包更新到最新版本,因为我需要<Fragment> 组件。 Fragment 是 React 16.x.x 引入的新特性
这是我当前的 package.json:
"dependencies": {
"@fortawesome/fontawesome-free": "^5.7.2",
"@types/prop-types": "^15.7.1",
"@types/react": "^16.8.17",
"@types/react-dom": "^16.8.4",
"electron-compile": "^6.4.4",
"electron-devtools-installer": "^2.2.4",
"electron-squirrel-startup": "^1.0.0",
"electron-stylus": "^0.1.1",
"ionicons": "^4.5.5",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-hot-loader": "^3.1.3",
"tslib": "^1.9.3"
},
"devDependencies": {
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"electron": "^5.0.1",
"electron-forge": "^5.2.4",
"electron-prebuilt-compile": "4.0.0",
"tslint": "^5.16.0",
"typescript": "^2.2.2"
}
我在 64 位的 Debian 机器上使用 VSCode 1.33.1,ESLint 1.8.2
错误代码:
JSX element type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null' is not a constructor function for JSX elements.
Type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>' is not assignable to type 'Element'.
Types of property 'type' are incompatible.
Type 'string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)' is not assignable to type 'string | ComponentClass<any> | StatelessComponent<any>'.
Type '(props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null' is not assignable to type 'string | ComponentClass<any> | StatelessComponent<any>'.
Type '(props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null' is not assignable to type 'StatelessComponent<any>'.
Type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null' is not assignable to type 'ReactElement<any>'.
Type 'null' is not assignable to type 'ReactElement<any>'.ts(2605)
补充说明:
当我将Fragment 作为单独的变量导入时:
import * as React from "react";
import { Fragment } from 'react'
然后尝试调用<Fragment>组件,还是报同样的错误
【问题讨论】:
-
如果你
import React, { Fragment } from 'react',只使用<Fragment>,会发生什么? -
@lux 还是一样的错误...我不明白为什么
-
@lux 我还添加了一些细节
-
不要导入 Fragment,请尝试使用
或 > -
@AlokMali : still same....
或 都显示相同的错误,其实我上面使用的图片是 。即使我可以成功运行代码。我可以禁用此警告吗?
标签: reactjs visual-studio-code electron eslint