【问题标题】:How to disable @typescript-eslint/explicit-function-return-type for some(), filter(), forEach()?如何为 some()、filter()、forEach() 禁用 @typescript-eslint/explicit-function-return-type?
【发布时间】:2019-11-16 08:10:21
【问题描述】:

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

每次都为some()filter()voidforEach() 声明一个boolean 返回类型非常烦人。

无效

[2, 5, 8, 1, 4].some(elem => elem > 10)

有效

[2, 5, 8, 1, 4].some((elem):boolean => elem > 10)

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

【问题讨论】:

    标签: typescript eslint


    【解决方案1】:

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

    {
      ...
      "plugins": ["@typescript-eslint"],
      "rules": {
        ...
        "@typescript-eslint/explicit-function-return-type": {
          "allowExpressions": true
        }
      }
    }
    

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

    【讨论】:

    • 现在改为"@typescript-eslint/explicit-function-return-type": "off"
    • @AO19 什么意思?
    【解决方案2】:

    这就是应该为规则@typescript-eslint/explicit-function-return-type配置.eslintrc的方式

    {
      "@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
              }
            ]
          }
        }
      ]
    }
    

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

    node.addEventListener('click', () => {});
    
    node.addEventListener('click', function() {});
    
    const foo = arr.map(i => i * i);
    

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

    【讨论】:

    • 天啊,经过 1 小时的谷歌搜索和阅读文档,我终于找到了解决方案。太感谢了!唯一的一点,"@typescript-eslint/explicit-function-return-type": "off" 应该在 rules 部分内。
    【解决方案3】:

    您必须在 .eslint 配置文件中将其设置为 "off"

    {
      ...
      "rules": {
        ...
        "@typescript-eslint/explicit-function-return-type": "off"
      }
    }
    

    【讨论】:

      【解决方案4】:
        rules: {
          'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
          'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
          "@typescript-eslint/explicit-module-boundary-types": {
            "allowExpressions": true
          }
        },
      

      【讨论】: