ESlint解析错误“意外令牌".在TypeScript强制转换运算符"as"上 [英] ESlint parsing error "Unexpected token" on TypeScript cast operator "as"
问题描述
任何出现的 as
运算符都会给出 [eslint] [E]解析错误:意外的令牌,预期的;"
指向 as的位置代码>.示例代码:
Any occurrence of as
operator gives [eslint] [E] Parsing error: Unexpected token, expected ";"
pointing to the place of as
. Example code:
{error && <small className="invalid-feedback">{(error as any).message}</small>}
此强制转换为 any
是针对 react-hooks-form
的 useFormContext
函数中某些错误的解决方法.
This cast to any
is a workaround to some bug in react-hooks-form
's useFormContext
function.
当我忽略该错误并编译应用程序时,它运行正常.
When I ignore the error and compile the app it works fine.
这发生在带有最新TypeScript和react-scripts的标准未注入的Create React App中:
This happens in a standard unejected Create React App with latest TypeScript and react-scripts:
$ 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除了自动生成的 tsconfig.json
外没有其他配置文件:
AFAIK there are no configuration files besides autogenerated 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?
推荐答案
我发现问题的根源是 Coc 的错误配置(Vim8的智能引擎)& Neovim)及其环境.它是在我的项目外部使用错误的(旧)安装 eslint .我必须更正 PATH 并确保正确检测到 workspace文件夹.
I've found that the source of the problem was misconfiguration of Coc (intellisense engine for Vim8 & Neovim) and its environment. It was using wrong (old) installation of eslint from outside of my project. I had to correct PATH and make sure workspace folders are detected correctly.
编辑:由 npm install添加的具有TypeScript支持的基于
尚未准备就绪,无法通过eslint插入.ts [x]源文件.必须手动配置它.我使用了此指南: https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/README.md 并提出以下配置( react-scripts
的应用程序-保存typescript @ types/node @ types/react @ types/react-dom @ types/jest .eslintrc.json
):
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. 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
):
{
"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"
}
}
]
}
可能需要大量调整.
这篇关于ESlint解析错误“意外令牌".在TypeScript强制转换运算符"as"上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!