首页 > 解决方案 > 解析错误:“parserOptions.project”已为@typescript-eslint/parser 设置。该文件与您的项目配置不匹配:.eslintrc.js

问题描述

我刚刚使用 create-react-app 启动了一个新的 typescript react 应用程序,然后安装了 firebase。我跑了firebase init,选择了打字稿选项,启用了 es lint 并启用了功能。

一旦我取消注释functions/index.ts 中的样板函数代码,我就注意到VS Code 中的一些警告......

函数/eslintrc.js:

在此处输入图像描述

给出错误:“解析错误:“parserOptions.project”已为 @typescript-eslint/parser 设置。该文件与您的项目配置不匹配:.eslintrc.js。该文件必须包含在至少一个提供的项目中"

函数/tsconfig.json:

在此处输入图像描述

函数/src/index.ts:

在此处输入图像描述

给出错误:“解析错误:'--jsx' 选项的参数必须是:'preserve'、'react-native'、'react'.eslint”

函数/package.json:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

我不明白这些错误。有人可以帮忙吗?

谢谢

标签: reactjstypescriptfirebasegoogle-cloud-functions

解决方案


TypeScript ESLint 的第一个错误与无法找到tsconfig.json与您的函数匹配的项目有关。

为了解决这个问题,我们需要tsconfig.json通过编辑你的parserOptions.

.eslintrc.js

// [...]
"parser": "@typescript-eslint/parser",
"parserOptions": {
  "project": [
    "./tsconfig.json",
    "./functions/tsconfig.json",
  ]
}
// [...]

要为 React 文件修复与 JSX 相关的解析问题,您必须将jsx编译器选项添加到tsconfig.json包含 JSX 的配置中。这可能是您的非功能配置,您很可能需要将其设置react-jsx为 React v17+ 或react在所有其他情况下为 Create React App。

tsconfig.json

{
  "compilerOptions": {
    "jsx": "react-jsx"
    // [...]
  }
  // [...]
}

推荐阅读