微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

@ typescript-eslint / no-unused-vars在类型声明中为假肯定

如何解决@ typescript-eslint / no-unused-vars在类型声明中为假肯定

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

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

我们在带有类型声明的字符串中遇到了 @ typescript-eslint / no-unused-vars 错误其中定义了“名称”,但从未使用。>

类型用法示例:

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配置有问题吗?

“夹板”:“ ^ 7.7.0”, “ @ typescript-eslint / eslint-plugin”:“ ^ 3.6.1”, “ @ typescript-eslint / parser”:“ ^ 4.0.1”, “ eslint-config-airbnb-typescript”:“ ^ 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","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","max-len": ["error",{ "code": 120 }],"react/prop-types": "off"
  }
}

解决方法

如果将@typescript-eslint/parser@typescript-eslint/eslint-plugin的版本更新为v4.1.0,则将能够使用使@typescript-eslint/no-unused-vars在所有情况下都能正常工作的最新更改。


顺便说一句-使用插件的v3.x但解析器的v4.x将使您进入一种真正的怪异状态,具有未定义和不受支持的行为。

您应该确保始终使用相同版本的两个软件包,因为每个版本都一起发布。

,

类似的问题请早先检查:Why ESLint throws 'no-unused-vars' for TypeScript interface?

在tsconfig.json中,禁用'@ typescript-eslint / no-unused-vars':0,然后改用“ strict”:true,“ noUnusedLocals”:true和“ noUnusedParameters”:true

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。