web-dev-qa-db-ja.com

some()、filter()、forEach()の@ TypeScript-eslint / explicit-function-return-typeを無効にする方法は?

無効にする方法 _@TypeScript-eslint/explicit-function-return-type_ for some()filter()forEach()

毎回some()およびfilter()boolean戻り値型を宣言し、forEach()voidを宣言するのは非常に面倒です。

無効

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

有効

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

このルールでエラーが発生することなく、最初のパターン(「無効」とマークされている)を使用できるようにしたいと思います。

9
Grzegorzg

あなたの.eslintrcファイルrulesの下に以下を追加できます:

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

allowExpressions のドキュメントによると、これにより、明示的な戻り値の型を宣言することなく、任意の関数にインラインコールバックを提供できます。

7
Patrick Roberts

これは、ルール.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
          }
        ]
      }
    }
  ]
}

{allowExpressions:true}を使用したこのルールの正しいコードの例:

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

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

const foo = arr.map(i => i * i);

詳細は allowExpressions のドキュメントを参照してください。

1
locropulenton