search

Home  >  Q&A  >  body text

eslint treats my *.ts files as javascript in a project that also has vue

<p>I am developing a web project using typescript and vue. After reading the documentation for typescript-eslint and eslint-plugin-vue, I've solved the following eslint configuration: </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>My vue files are parsed correctly, but my ts files have unreasonable lint errors. It seems they are checked against javascript syntax. As shown below:</p> <p>If I remove <code>parserOptions</code> and change the parser to <code>@typescript-eslint/parser</code>, the ts file lints correctly, but the lint of the vue file breaks . </p> <p>Does anyone know? </p>
P粉739706089P粉739706089458 days ago480

reply all(2)I'll reply

  • P粉236743689

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

    As far as I know, you have two options:

    1. Change parser: "vue-eslint-parser" to @typescript-eslint/parser

    2. Add overrides for .ts files and set the parser to @typescript-eslint/parser

    reply
    0
  • P粉038856725

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

    I guess I'm too tired or too old, anyway, the problem is that there are two parserOptions sections in the same lint file. This is the final working version:

    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"],
    };
    

    reply
    0
  • Cancelreply