11

Hi everyone I am migrating my vue3 project from js to typescript, I run into this problem :

enter image description here

Here is my code in .vue file

<script setup lang="ts">
const toto = (msg: string) => {
  console.log(msg)
}
</script>

And here is my eslintrc.js

module.exports = {
  'env': {
    'browser': true,
    'es2021': true
  },
  'extends': [
    'eslint:recommended',
    'plugin:vue/vue3-essential'
  ],
  'parserOptions': {
    'ecmaVersion': 13,
    'sourceType': 'module'
  },
  'plugins': [
    'vue'
  ],
  'rules': {
    'vue/multi-word-component-names': 'off',
    'vue/object-curly-spacing': [2, 'always'],
    'vue/html-closing-bracket-spacing': [2, {
      'selfClosingTag': 'always'
    }],
    'vue/max-attributes-per-line': [2, {
      'singleline': {
        'max': 1
      },
      'multiline': {
        'max': 1
      }
    }],
    'semi': [2, 'never']
  }
}
2
  • "(and I wanna die), I am a newbie to typescript". Please edit your question to remove information not inherent to the question itself Commented Nov 23, 2021 at 14:58
  • 2
    It looks like your eslint is not set up for parsing typescript. There's some docs here on how to configure it: typescript-eslint.io/docs/linting/linting Commented Nov 23, 2021 at 16:23

5 Answers 5

12

You need to configure eslint to support typescript as eslint doesn't support it out of the box. First, you need to install @typescript-eslint/parser and then @typescript-eslint/eslint-plugin. Once you have installed these, update your config as follows-

module.exports = {
    'env': {
        'browser': true,
        'es2021': true,
        node: true
    },
    'extends': [
        'eslint:recommended',
        'plugin:vue/vue3-essential'
    ],
    'parserOptions': {
        'ecmaVersion': 12,
        'sourceType': 'module',
        parser: '@typescript-eslint/parser'
    },
    'plugins': [
        'vue',
        '@typescript-eslint'
    ],
    'rules': {
        'vue/multi-word-component-names': 'off',
        'vue/object-curly-spacing': [2, 'always'],
        'vue/html-closing-bracket-spacing': [2, {
            'selfClosingTag': 'always'
        }],
        'vue/max-attributes-per-line': [2, {
            'singleline': {
                'max': 1
            },
            'multiline': {
                'max': 1
            }
        }],
        'semi': [2, 'never']
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

by doing as u said, i'm able to lint my vue file but not my *.ts file, I get an error like that : Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
1

In my case, the problem was that I was using the parser option as an array, instead of a string:

   parserOptions: {
-    parser: ['@typescript-eslint/parser'],
+    parser: '@typescript-eslint/parser',
   },

Comments

0

If you use both JS and TS in a project, this config helps

  overrides: [
     {
       files: ['*.vue'],
       parser: 'svelte-eslint-parser',
       parserOptions: {
         parser: {
           // Specify a parser for each lang.
           ts: '@typescript-eslint/parser',
           js: 'espree',
           typescript: '@typescript-eslint/parser'
         }
       }
     }
   ],

Comments

0

So I was getting this same error for a big project which has an app and multiple libraries. In each library I have a small skeleton eslint.config.js file which pulls-in the common eslint.config.js file which is held at the root, alongside the /project folder.

The error went away when I noticed that I had an

ignores: ['**/*.spec.ts', '**/*.mock.ts'],

in the outer file at the project level, but I had commented it out at the library level. It probably didn't help either that I had a

files: ['**/*.ts'],

at both levels. Anyway, point being. If the error only happens in .spec.ts files. Have a look at simplifying any config files' ignores commands.

Comments

-1

I had this problem on node v12.22.9. By upgrading to v14.21.2, I no longer had the parsing error. You can upgrade/install with the command

nvm install v14.21.2 

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.