@tyescript-eslint/no-unuse-vars类型声明中的假阳性 [英] @typescript-eslint/no-unused-vars false positive in type declarations

查看:26
本文介绍了@tyescript-eslint/no-unuse-vars类型声明中的假阳性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@typescript-eslint/no-unused-vars.有一个问题 因此,我们有类型

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

并且我们在类型声明的字符串中出现@typescript-eslint/no-unused-vars错误,它表明‘name’已定义但从未使用

类型用法示例:

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

或:

interface CheckboxPropsType {
    value: string,
    onClick(value: string): void
}

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

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

";eslint";:";^7.7.0";, ";@typescript-eslint/eslint-plugin";:;^3.6.1&q;, ";@type-eslint/parser";:^4.0.1";, ";eslint-config-airbnb-tyescript";:";^9.0.0";,

{
  "extends": [
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module",
    "project": "./tsconfig.json"
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "plugins": ["@typescript-eslint", "react-hooks", "react"],
  "env": {
    "browser": true
  },
  "rules": {
    "object-curly-newline": 0,
    "import/named": ["error"],
    "indent": ["error", 4],
    "react/jsx-indent": ["error", 4],
    "comma-dangle": ["error", "never"],
    "import/prefer-default-export": "off",
    "react/jsx-fragments": "off",
    "arrow-body-style": "off",
    "object-curly-spacing": "off",
    "@typescript-eslint/indent": ["error", 4, {"SwitchCase": 1}],
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "no-undef": "error",
    "react/jsx-indent-props": ["error", 4],
    "max-len": ["error", { "code": 120 }],
    "react/prop-types": "off"
  }
}

推荐答案

前面提出的类似问题检查:Why ESLint throws 'no-unused-vars' for TypeScript interface?

禁用'@typescript-eslint/no-unused-vars': 0规则,改用tsconfig.json中的"strict": true"noUnusedLocals": true"noUnusedParameters": true

这篇关于@tyescript-eslint/no-unuse-vars类型声明中的假阳性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆