简体   繁体   中英

Eslint no-unused-vars error shows for used interface in Vue

I'm using Vue with Vue CLI and Typescript.
I imported interface from vuex file and used it for mapState type annotation.
But eslint shows me an error.

'State' is defined but never used. eslint(no-unused-vars)


Code

import { Component, Vue } from 'vue-property-decorator';
import { mapState } from 'vuex';
import { State } from '@/store/index';

@Component({
  computed: mapState<State>({
    cards: (state: State) => state.user.cards,
  })
})
export default class Home extends Vue {}

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true
  },
  'extends': [
    'plugin:vue/essential',
    'eslint:recommended',
    '@vue/typescript'
  ],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'quotes': ['error', 'single'],
    'semi': ['warn', 'always']
  },
  parserOptions: {
    parser: '@typescript-eslint/parser'
  }
};


What should I do not to see the eslint error?

Solution:

EsLint default no-unused-vars has an bug, that reproduces like you explain. According to my source [1] it should be fixed by using @typescript-eslint/no-unused-vars like so:

overrides: [
   // Fix no-used-vars when importing ts types in .vue files
   {
     files: ["*.vue"],
     rules: {
        'no-unused-vars': 'off',
        '@typescript-eslint/no-unused-vars': 'error'
     }
  }
]

Source [2] is another question on same topic. There, my solution [1] is one option among others. In [2] and [3] it is said you need the @typescript-eslint/eslint-plugin and @typescript-eslint/parser installed like so:

npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

Source:

[1] https://github.com/vuejs/eslint-config-typescript/issues/14

[2] ESLint - Configuring "no-unused-vars" for TypeScript

[3] https://khalilstemmler.com/blogs/typescript/eslint-for-typescript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM