TSLint:禁止非箭头函数

问题描述 投票:0回答:2

我正在使用

typescript
yarn
react
。我想在我的应用程序的某些地方使用非箭头函数,如下所示

function myFunction () {
  console.log('This is a traditional function')
}

我的 TSLint 不允许我这样做。当我运行

yarn lint
命令来捕获所有 linting 错误时,我得到

ERROR :  non-arrow functions are forbidden

这是我的

tslint.json
文件。请告知我如何摆脱这个错误。

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:recommended",
        "tslint-config-standard",
        "tslint-react",
        "tslint-config-prettier"
    ],
    "jsRules": {},
    "rules": {
        "ordered-imports": false,
        "jsx-no-lambda": false,
        "object-literal-sort-keys": false,
        "jsx-boolean-value": false,
        "indent": [
            true,
            "spaces",
            2
        ],
        "align": [
            true,
            "parameters",
            "statements"
        ],
        "ban": false,
        "class-name": true,
        "comment-format": [
            true,
            "check-space"
        ],
        "curly": false,
        "eofline": true,
        "forin": true,
        "linebreak-style": [
            true,
            "LF"
        ],
        "interface-name": false,
        "jsdoc-format": true,
        "label-position": true,
        "max-line-length": [
            true,
            140
        ],
        "member-ordering": [
            true,
            "public-before-private",
            "static-before-instance",
            "variables-before-functions"
        ],
        "no-any": false,
        "no-arg": true,
        "no-bitwise": true,
        "no-console": [
            false,
            "debug",
            "info",
            "time",
            "timeEnd",
            "trace",
            "log"
        ],
        "no-construct": true,
        "no-constructor-vars": false,
        "no-debugger": true,
        "no-shadowed-variable": true,
        "no-duplicate-variable": true,
        "no-empty": true,
        "no-eval": true,
        "no-internal-module": true,
        "no-require-imports": true,
        "no-string-literal": true,
        "no-switch-case-fall-through": true,
        "trailing-comma": {
            "singleline": "never",
            "multiline": "always"
        },
        "no-trailing-whitespace": true,
        "no-unnecessary-type-assertion": false,
        "no-var-keyword": true,
        "no-var-requires": false,
        "no-unused-expression": [true, "allow-fast-null-checks"],
        "one-line": [
            true,
            "check-open-brace",
            "check-catch",
            "check-else",
            "check-whitespace"
        ],
        "quotemark": [
            true,
            "single",
            "jsx-double",
            "avoid-escape"
        ],
        "radix": true,
        "semicolon": [
            true,
            "always"
        ],
        "switch-default": true,
        "triple-equals": [
            true,
            "allow-null-check"
        ],
        "typedef": [
            false,
            "call-signature",
            "parameter",
            "property-declaration",
            "member-variable-declaration"
        ],
        "typedef-whitespace": [
            true,
            {
                "call-signature": "nospace",
                "index-signature": "nospace",
                "parameter": "nospace",
                "property-declaration": "nospace",
                "variable-declaration": "nospace"
            }
        ],
        "use-strict": [
            false,
            "check-module",
            "check-function"
        ],
        "variable-name": [
            true,
            "ban-keywords"
        ],
        "whitespace": [
            true,
            "check-branch",
            "check-decl",
            "check-operator",
            "check-separator",
            "check-type"
        ]
    },
    "rulesDirectory": []
}

注意:我在某些地方使用了箭头函数,并且运行良好。我想在某些函数中使用

this
关键字,因此也尝试使用传统函数。我无法用传统函数替换箭头函数,反之亦然。

reactjs typescript tslint yarn-v2
2个回答
3
投票

您正在扩展的默认值之一似乎应用了此规则。您可以通过禁用

"only-arrow-functions"
来覆盖它,它应该可以工作:

{
  "rules": {
    "only-arrow-functions": false,
    ...
  }
  ...
}

根据您提供的信息,这似乎可行。


0
投票

在类中仅强制执行非箭头函数并避免任何序列化错误。有一个规则

{
  "rules": {
      "no-restricted-syntax": [ "error",
      {
        "selector": "ClassBody>PropertyDefinition>ArrowFunctionExpression",
        "message": "class properties as arrow functions are restricted to avoid serialisation errors"
      }
    ]
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.