【问题标题】:React Typescript Type Cast Issue - Parsing Error: Missing SemicolonReact Typescript 类型转换问题 - 解析错误:缺少分号
【发布时间】:2021-07-28 11:02:27
【问题描述】:

我有这行简单的代码:

const target = e.target as HTMLLIElement;

我收到此错误:

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

我查看了数十篇与我的问题相似的不同帖子,但没有一个与我的问题完全匹配,或者他们的解决方案与我的项目并不真正相关。例如,有些遇到了 eslint 问题,但我没有安装 eslint 包。这是我的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。我已尝试将分号添加到与此错误相同的文件中的所有表达式。

关于如何解决此问题的任何想法?

【问题讨论】:

    标签: javascript reactjs typescript eslint tsconfig


    【解决方案1】:

    应该是来自eslint的错误。

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

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

    【讨论】:

    • 那行得通,但我不知道为什么,我什至没有使用 eslint :P
    • create-react-app 带有 eslint。你可以查看这个链接:stackoverflow.com/questions/59633005/…
    • 它对我不起作用.... :(。我正在逐步从 js 迁移到 ts。是这个原因吗?
    【解决方案2】:

    只需将"parser": "@typescript-eslint/parser", 放入您的 eslint 配置文件中即可。

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2017-06-08
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2020-08-08
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多