TSLint: non-arrow functions are forbidden

I’m using typescript, yarn, react. I want to use non-arrow functions in some places of my app like this

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

and my TSLint will not allow me to do that. When I run yarn lint command to catch all the linting errors, I get

ERROR : non-arrow functions are forbidden

This is my tslint.json file. Please advise on how I can get rid of this error.

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

Note: I’m using arrow functions in some places and it is working fine. I want to use this keyword in some functions so trying to use traditional functions as well. I can’t replace arrow functions with traditional functions and vice-versa.

>Solution :

It appears one of the defaults you’re extending has applied this rule. You can override it by disabling "only-arrow-functions" and it should be working:

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

With the info you provided, this seems to work.

Leave a Reply