React Typescript类型转换问题-分析错误:缺少分号



我有一行简单的代码:

const target = e.target as HTMLLIElement;

我得到了这个错误:

Line 18:28:  Parsing error: Missing semicolon
> 18 |     const target = e.target as HTMLLIElement;
|                            ^

我看了几十个不同的帖子,它们与我的问题相似,但没有一个能完全匹配我的问题,或者它们的解决方案与我的项目无关。例如,有些人遇到了esint问题,但我没有安装esint包。这是我的package.json文件:

{
"name": "ally-autobot",
"version": "0.1.0",
"private": true,
"dependencies": {
"@craco/craco": "^6.1.1",
"@reduxjs/toolkit": "^1.5.1",
"@tailwindcss/postcss7-compat": "^2.1.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/lodash": "^4.14.168",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-redux": "^7.1.7",
"@types/react-router-dom": "^5.1.7",
"autoprefixer": "^9",
"firebase": "^8.4.2",
"firebase-tools": "^9.10.0",
"lint": "^0.7.0",
"lodash-core": "^4.17.19",
"postcss": "^7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-firebase-hooks": "^3.0.4",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"redux-persist": "^6.0.0",
"source-map-loader": "^2.0.1",
"tailwindcss": "npm:@tailwindcss/postcss7-compat",
"typescript": "~4.1.5"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

这是我的tsconfig.json文件:

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

我也尝试过使用尖括号方法,但这不起作用,因为我在tsx文件中。我已尝试将noImplicitAny设置为false。我已经尝试将分号添加到与此错误相同的文件中的所有表达式中。

对如何解决这个问题有什么想法吗?

应该是esint中的错误。

将eslintConfig添加到package.json可能会解决您的问题。以下设置是create-react应用的默认eslintConfig

"dependencies": {
...
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},

只需将"parser": "@typescript-eslint/parser",放在eslint配置文件中,就可以了。

最新更新