首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ type typescript eslint/no-unused vars在类型声明中出现假阳性

@ type typescript eslint/no-unused vars在类型声明中出现假阳性
EN

Stack Overflow用户
提问于 2020-09-06 17:49:50
回答 4查看 27.6K关注 0票数 51

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

代码语言:javascript
复制
type SomeType = (name: string) => void;

我们在字符串中有@typescript-eslint/no-unused-vars错误和类型声明,表示“名称”是定义的,但从未使用过

类型使用示例:

代码语言:javascript
复制
export const LogSomeInfo: SomeType = (name: string) => {
    const a = name;
    console.log(a);
};

或者:

代码语言:javascript
复制
interface CheckboxPropsType {
    value: string,
    onClick(value: string): void
}

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

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

"@typescript-eslint/eslint-plugin":"^3.6.1",“@-eslint/解析器”:"^4.0.1",“eslint airbnb-typescript”:"^9.0.0",

代码语言:javascript
复制
{
  "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"
  }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-09-06 18:11:39

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

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

票数 28
EN

Stack Overflow用户

发布于 2020-09-08 17:10:01

资料来源:我是typescript-eslint项目的维护者。

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

顺便说一句,使用插件的v3.x,但是解析器的v4.x将使您处于一个非常奇怪的状态,没有定义和不受支持的行为。

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

票数 34
EN

Stack Overflow用户

发布于 2022-05-10 07:18:16

只需确保您有以下最新版本:

  • typescript-eslint
  • @typescript-eslint/parser
  • @typescript-eslint/eslint-plugin

.eslintrc.js中,确保将下列行添加到rules部分:

代码语言:javascript
复制
  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": ["error"]
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63767199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档