如何为 some()、filter()、forEach() 禁用 @typescript-eslint/explicit-function-return-type?

Grz*_*rzg 20 typescript eslint

如何禁用@typescript-eslint/explicit-function-return-typesome()filter()forEach()

每次都booleansome()andfilter()voidfor声明一个返回类型是很烦人的forEach()

无效的

[2, 5, 8, 1, 4].some(elem => elem > 10)
Run Code Online (Sandbox Code Playgroud)

有效的

[2, 5, 8, 1, 4].some((elem):boolean => elem > 10)
Run Code Online (Sandbox Code Playgroud)

我希望能够使用第一个模式(标记为“无效”)而不会从此规则中出错。

Pat*_*rts 23

在您的.eslintrc文件中,您可以添加以下内容rules

{
  ...
  "plugins": ["@typescript-eslint"],
  "rules": {
    ...
    "@typescript-eslint/explicit-function-return-type": {
      "allowExpressions": true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

根据关于allowExpressions的文档,这将允许您向任何函数提供内联回调,而无需声明显式返回类型。

  • 现在更改为 `"@typescript-eslint/explicit-function-return-type": "off"` (5认同)

loc*_*ton 9

这是.eslintrc应该如何配置规则@typescript-eslint/explicit-function-return-type

{
  "@typescript-eslint/explicit-function-return-type": "off",
  "overrides": [
    {
      "files": ["*.ts", "*.tsx"],
      "parser": "@typescript-eslint/parser",
      ...
      "rules": {
        ...
        "@typescript-eslint/explicit-function-return-type": [
          "error",
          {
            "allowExpressions": true
          }
        ]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

此规则的正确代码示例为 { allowExpressions: true }:

node.addEventListener('click', () => {});

node.addEventListener('click', function() {});

const foo = arr.map(i => i * i);
Run Code Online (Sandbox Code Playgroud)

更多信息请参阅allowExpressions 的文档。

  • 天哪,经过 1 小时的谷歌搜索和阅读文档,我终于找到了解决方案。太感谢了!唯一的事情是,`"@typescript-eslint/explicit-function-return-type": "off"` 应该在 `rules` 部分内。 (6认同)