![](/img/trans.png)
[英]React typescript eslint error Parsing error: Unexpected token ! How to fix this?
[英]ESlint parsing error "Unexpected token" on TypeScript cast operator "as"
任何出現的as
運算符都會給出[eslint] [E] Parsing error: Unexpected token, expected ";"
指向as
的位置。 示例代碼:
{error && <small className="invalid-feedback">{(error as any).message}</small>}
這種轉換為any
是對react-hooks-form
的useFormContext
函數中某些錯誤的一種解決方法。
當我忽略錯誤並編譯應用程序時,它工作正常。
這發生在帶有最新 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 除了自動生成的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"
]
}
任何想法為什么會發生這種情況?
我遇到了類似的問題,對我有用的簡單修復如下。
我將此添加到我的package.json
:
"eslintConfig": {
"extends": [
"react-app"
]
},
我注意到create-react-app --template typescript
生成了一個package.json
有這個。
我發現問題的根源在於Coc (Vim8 和 Neovim 的智能感知引擎)及其環境的錯誤配置。 它在我的項目之外使用了錯誤的(舊的) eslint安裝。 我必須更正PATH並確保正確檢測到工作區文件夾。
編輯:基於react-scripts
的應用程序,通過npm install --save typescript @types/node @types/react @types/react-dom @types/jest
添加了 TypeScript 支持還沒有准備好通過 eslint 對 .ts[x] 源文件進行 linting . 必須手動配置它。 我使用了本指南: 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"
}
}
]
}
這可能需要大量調整。
我在嘗試使用帶有as
運算符的 tsx 文件的 Jest 時遇到了這個問題。 我通過做兩件簡單的事情解決了這個問題:
npm install --save-dev @babel/preset-typescript
'@babel/preset-typescript'
。任何出現的as
運算符都會給出[eslint] [E] Parsing error: Unexpected token, expected ";"
指向as
的地方。 示例代碼:
{error && <small className="invalid-feedback">{(error as any).message}</small>}
此鑄造到any
是一種變通方法在一些bug react-hooks-form
的useFormContext
功能。
當我忽略該錯誤並編譯應用程序時,它可以正常工作。
這是在帶有最新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除了自動生成的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"
]
}
任何想法為什么會發生這種情況?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.