相关疑难解决方法(0)

@typescript-eslint/no-unused-vars 类型声明中的误报

@typescript-eslint/no-unused-vars存在一个问题。所以,我们有类型

type SomeType = (name: string) => void;

Run Code Online (Sandbox Code Playgroud)

我们在带有类型声明的字符串中有@typescript-eslint/no-unused-vars错误,它说 'name' is defined but never used。

类型用法示例:

export const LogSomeInfo: SomeType = (name: string) => {
    const a = name;
    console.log(a);
};

Run Code Online (Sandbox Code Playgroud)

或者:

interface CheckboxPropsType {
    value: string,
    onClick(value: string): void
}
Run Code Online (Sandbox Code Playgroud)

并且 eslint 在 onClick... 字符串处中断,表示该值已定义但从未使用过。即使类型分配正确且实际的 onClick 处理程序使用该值!

问题:这条规则有什么问题,为什么会在这种情况下触发。为什么 eslint 将类型/接口中函数的类型声明识别为常规函数?我的 eslint 配置有问题吗?

"eslint": "^7.7.0", "@typescript-eslint/eslint-plugin": "^3.6.1", "@typescript-eslint/parser": "^4.0.1", "eslint-config- airbnb 打字稿": "^9.0.0",

{
  "extends": [
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": { …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs eslint typescript-eslint

22
推荐指数
3
解决办法
1万
查看次数

对于 no-unused-vars,我如何在 ESLint 中获得警告而不是错误?

使用 ESLint 时如何获得警告而不是错误?

我使用6.7.2带有 babel 和 react 插件的ESLint版本。

这是我的实际.eslintrc.json:

{
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "react"
    ],
    "rules": {
    },
    "settings": {
        "react" : {
            "version": "16.7.0"
        }
    },
    "parser": "babel-eslint"
}
Run Code Online (Sandbox Code Playgroud)

eslint

8
推荐指数
1
解决办法
2449
查看次数

Eslint no-unused-vars 错误显示在 Vue 中使用的接口

我在 Vue CLI 和 Typescript 中使用 Vue。
我从 vuex 文件导入接口并将其用于mapState类型注释。
但是 eslint 显示了一个错误。

'State' is defined but never used. eslint(no-unused-vars)
Run Code Online (Sandbox Code Playgroud)


代码

'State' is defined but never used. eslint(no-unused-vars)
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js

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 {}
Run Code Online (Sandbox Code Playgroud)


我应该怎么做才能看到 eslint 错误?

typescript vue.js eslintrc

6
推荐指数
1
解决办法
523
查看次数