简体   繁体   English

ESlint 在 TypeScript 转换运算符“as”上解析错误“意外标记”

[英]ESlint parsing error "Unexpected token" on TypeScript cast operator "as"

Any occurrence of as operator gives [eslint] [E] Parsing error: Unexpected token, expected ";"任何出现的as运算符都会给出[eslint] [E] Parsing error: Unexpected token, expected ";" pointing to the place of as .指向as的位置。 Example code:示例代码:

{error && <small className="invalid-feedback">{(error as any).message}</small>}

This cast to any is a workaround to some bug in react-hooks-form 's useFormContext function.这种转换为any是对react-hooks-formuseFormContext函数中某些错误的一种解决方法。

When I ignore the error and compile the app it works fine.当我忽略错误并编译应用程序时,它工作正常。

This happens in a standard unejected Create React App with latest TypeScript and react-scripts:这发生在带有最新 TypeScript 和 react-scripts 的标准未弹出 Create React App 中:

$ npm list -dev -depth 0
frontend@0.1.0 
├── eslint-plugin-flowtype@3.13.0
├── react-hot-loader@4.12.19
├── react-scripts@3.4.0
└── typescript@3.8.2

AFAIK there are no configuration files besides autogenerated tsconfig.json : AFAIK 除了自动生成的tsconfig.json之外没有配置文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Any ideas why this happens?任何想法为什么会发生这种情况?

I was running into a similar problem, and a simple fix that worked for me was the following.我遇到了类似的问题,对我有用的简单修复如下。
I added this to my package.json :我将此添加到我的package.json

  "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },

I noticed that create-react-app --template typescript generates a package.json with this in it.我注意到create-react-app --template typescript生成了一个package.json有这个。

I've found that the source of the problem was misconfiguration of Coc (intellisense engine for Vim8 & Neovim) and its environment.我发现问题的根源在于Coc (Vim8 和 Neovim 的智能感知引擎)及其环境的错误配置 It was using wrong ( old ) installation of eslint from outside of my project.它在我的项目之外使用了错误的(旧的eslint安装。 I had to correct PATH and make sure workspace folders are detected correctly.我必须更正PATH并确保正确检测到工作区文件夹

EDIT : react-scripts based application with TypeScript support added by npm install --save typescript @types/node @types/react @types/react-dom @types/jest is not ready for linting .ts[x] source files by eslint.编辑:基于react-scripts的应用程序,通过npm install --save typescript @types/node @types/react @types/react-dom @types/jest添加了 TypeScript 支持还没有准备好通过 eslint 对 .ts[x] 源文件进行 linting . One have to manually configure it.必须手动配置它。 I used this guide: https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/README.md and came up with following config ( .eslintrc.json ):我使用了本指南: https : //github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/README.md并提出了以下配置( .eslintrc.json ):

{
    "extends": [
        "standard",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "plugins": [
        "@typescript-eslint"
    ],
    "settings": {
        "react": {
            "pragma": "React",
            "version": "detect",
            "flowVersion": "0.53"
        }
    },
    "overrides": [
        {
            "files": ["*.js", "*.jsx"],
            "rules": {
                "@typescript-eslint/explicit-function-return-type": "off"
            }
        }
    ]
}

Which probably will need a lot of tuning.这可能需要大量调整。

I was running into this issue while trying to use Jest with a tsx file with the as operator in it.我在尝试使用带有as运算符的 tsx 文件的 Jest 时遇到了这个问题。 I solved the problem by doing two simple things:我通过做两件简单的事情解决了这个问题:

  1. Run npm install --save-dev @babel/preset-typescript运行npm install --save-dev @babel/preset-typescript
  2. In your babel.config.js, in the presets array, add '@babel/preset-typescript' at the end.在您的 babel.config.js 中,在预设数组中,在末尾添加'@babel/preset-typescript'

Any occurrence of as operator gives [eslint] [E] Parsing error: Unexpected token, expected ";"任何出现的as运算符都会给出[eslint] [E] Parsing error: Unexpected token, expected ";" pointing to the place of as .指向as的地方。 Example code:示例代码:

{error && <small className="invalid-feedback">{(error as any).message}</small>}

This cast to any is a workaround to some bug in react-hooks-form 's useFormContext function.此铸造到any是一种变通方法在一些bug react-hooks-formuseFormContext功能。

When I ignore the error and compile the app it works fine.当我忽略该错误并编译应用程序时,它可以正常工作。

This happens in a standard unejected Create React App with latest TypeScript and react-scripts:这是在带有最新TypeScript和react-scripts的标准未创建的Create React App中发生的:

$ npm list -dev -depth 0
frontend@0.1.0 
├── eslint-plugin-flowtype@3.13.0
├── react-hot-loader@4.12.19
├── react-scripts@3.4.0
└── typescript@3.8.2

AFAIK there are no configuration files besides autogenerated tsconfig.json : AFAIK除了自动生成的tsconfig.json外,没有其他配置文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Any ideas why this happens?任何想法为什么会发生这种情况?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM