首頁  >  問答  >  主體

eslint 將我的 *.ts 檔案視為也有 vue 的專案中的 javascript

<p>我正在使用 typescript 和 vue 開發一個網頁專案。在閱讀了 typescript-eslint 和 eslint-plugin-vue 的文檔後,我已經解決了以下 eslint 配置:</p> <pre class="brush:php;toolbar:false;">module.exports = { root: true, env: { browser: true, es2021: true, }, extends: [ "eslint:recommended", "plugin:vue/vue3-essential", "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-requiring-type-checking", "prettier", ], parser: "vue-eslint-parser", parserOptions: { parser: "@typescript-eslint/parser", project: ["./tsconfig.json"], tsconfigRootDir: __dirname, extraFileExtensions: [".vue"], }, overrides: [], parserOptions: { ecmaVersion: "latest", sourceType: "module", }, plugins: ["vue", "@typescript-eslint"], };</pre> <p>我的 vue 檔案解析正確,但我的 ts 檔案有不合理的 lint 錯誤。看來它們是針對 javascript 語法進行檢查的。如下圖所示:</p> <p>如果我刪除<code>parserOptions</code> 並將解析器更改為<code>@typescript-eslint/parser</code>,ts 檔案會正確lint,但vue 檔案的lint 會中斷。 </p > <p>有人知道嗎? </p>
P粉739706089P粉739706089383 天前408

全部回覆(2)我來回復

  • P粉236743689

    P粉2367436892023-09-03 13:09:12

    據我所知,您有兩個選擇:

    1. 解析器:「vue-eslint-parser」改為@typescript-eslint/parser

    2. 為 .ts 檔案新增覆蓋,並將解析器設定為 @typescript-eslint/parser

    回覆
    0
  • P粉038856725

    P粉0388567252023-09-03 00:38:58

    我想我太累了或太老了,無論如何,問題在於同一個 lint 檔案中有兩個 parserOptions 部分。這是最終的工作版本:

    module.exports = {
      root: true,
      env: {
        browser: true,
        es2021: true,
      },
      extends: [
        "eslint:recommended",
        "plugin:vue/vue3-essential",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
        "prettier",
      ],
      parser: "vue-eslint-parser",
      parserOptions: {
        parser: "@typescript-eslint/parser",
        project: ["./tsconfig.json"],
        ecmaVersion: "latest",
        sourceType: "module",
        extraFileExtensions: [".vue"],
      },
    //  parserOptions: {
    //    ecmaVersion: "latest",
    //    sourceType: "module",
    //  },
      plugins: ["vue", "@typescript-eslint"],
    };
    

    回覆
    0
  • 取消回覆