代码之家  ›  专栏  ›  技术社区  ›  DennyHiu

反应Visual Studio代码中的片段错误

  •  0
  • DennyHiu  · 技术社区  · 5 年前

    即使它工作正常,VisualStudio代码仍会不断显示错误,如下面的屏幕截图所示。抱歉,我的埃斯林似乎不明白 React.Fragment 。它会向任何用户显示错误 <React.Fragment> <Fragment> 我有。

    如何禁用或修复此问题?

    这是一个电子反应项目,带有打字脚本。我是从

    electron-forge init my-new-project --template=react-typescript
    

    然后我更新了我的 react , @types/react , @types/react-dom react-dom 包到他们的最新版本,因为我需要 <碎片> 组成部分片段是React 16引入的一个新特性。x、 x

    这是我目前的包裹。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"
      }
    

    我在Debian机器64位ESLint 1.8.2上使用VSCode 1.33.1

    错误代码:

    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)
    

    error in visual studio code

    补充说明: 当我导入 Fragment 作为一个单独的变量:

    import * as React from "react";
    import { Fragment } from 'react' 
    

    然后试着打电话给 <碎片> 组件,它仍然抛出相同的错误

    0 回复  |  直到 5 年前
        1
  •  2
  •   amateur barista    5 年前

    我还在电子应用中使用React。在后端安装Babel和Webpack进行编译。

    我写了一篇关于我目前使用的堆栈以及如何解决语法突出问题的帖子。

    请注意,正如我想你提到的,我的应用程序实际上是在Webpack/Babel中正确编译的。只是Visual Studio代码引发了一些问题。

    简而言之,将VSC中的语言从 Javascript Typescript 修正了突出显示的问题。

    以下是我的帖子: https://medium.com/@callback.insanity/using-react-fragments-in-visual-studio-code-2019-d19af4469788