简体   繁体   中英

ESLint un-used varialbes rule for parameters of function type in TypeScript

I'm using ESLint with TypeScript. When I try to create a function type with some required parameters, ESLint shows error eslint: no-unused-vars .

type Func = (paramA: number) => void Here, paramA is un-used variable according to ESLint.

My .eslintrc.json file

{
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "rules": {
    }
}

So, what is then, the correct way to create a function type in TypeScript with ESLint?

Thanks in advance

As mentioned in the document of typescript-eslint , disabled the rule from eslint and enable from typescript-eslint.

{
  // note you must disable the base rule as it can report incorrect errors
  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": ["error"]
}

If I understood you correctly, what you're trying to do is

const func: (paramA: number) => void

to define a function type.

Actually, they have answered to this question in their FAQ

I needed to turn off eslint's, no-unused-vars rule, and turn on the typescript-eslint rule.

"rules": {
  "no-unused-vars": "off",
  "@typescript-eslint/no-unused-vars": "error"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM