typescript怎么定义回调函数的参数,eslint检查报no-unused-vars

比如有个函数:

const fn = (str: string, callback: (param: string, result: boolean) => void) => {
    callback(str, true);
};

fn('test', () => {
    //
});

代码规范检查就会报callback的参数

'param' is defined but never used   no-unused-vars
'result' is defined but never used  no-unused-vars

.eslintrc.js

module.exports = {
    root: true,
    env: {
        browser: false,
        es6: true,
        node: true,
        commonjs: true
    },
    extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaVersion: 11,
        sourceType: 'module'
    },
    plugins: ['@typescript-eslint'],
    rules: {
        '@typescript-eslint/no-explicit-any': 2,
        '@typescript-eslint/no-this-alias': [
            'error',
            {
                allowDestructuring: true,
                allowedNames: ['self']
            }
        ],
        'indent': [2, 4, { SwitchCase: 1 }],
        'linebreak-style': [0, 'error', 'windows', 'unix'],
        'quotes': [2, 'single'],
        'no-caller': 2,
        'semi': ['error', 'always'],
        'no-multiple-empty-lines': [2, { 'max': 2 }],
        'no-console': 2,
        'no-constant-condition': 2,
        'no-extra-parens': 2,
        'no-extra-semi': 2,
        'no-func-assign': 2,
        'no-mixed-spaces-and-tabs': [2, false],
        'no-trailing-spaces': 1,
        'camelcase': 2,
        'comma-dangle': [2, 'never'],
        'consistent-this': [2, 'self'],
        'no-multi-spaces': 1,
        'no-multi-str': 2,
        'no-redeclare': 2,
        'no-undef': 2,
        'no-sparse-arrays': 2,
        'no-unreachable': 2,
        'no-unused-expressions': 2,
        'no-unused-vars': [2, { 'vars': 'all', 'args': 'after-used' }],
        'no-use-before-define': 2,
        'no-extra-boolean-cast': 2,
        'no-void': 2,
        'no-var': 2,
        'brace-style': [1, '1tbs'],
        'arrow-spacing': 0,
        'comma-spacing': [2, { 'before': false, 'after': true }],
        'comma-style': [2, 'last'],
        'curly': [2, 'all'],
        'default-case': 2,
        'dot-notation': [0, { 'allowKeywords': true }],
        'eqeqeq': 2,
        'generator-star-spacing': 0,
        'init-declarations': 2,
        'key-spacing': [2, { 'beforeColon': false, 'afterColon': true }],
        'newline-after-var': 2,
        'id-match': 0,
        'semi-spacing': [0, { 'before': false, 'after': true }],
        'sort-vars': 0,
        'space-before-function-paren': [0, 'always'],
        'strict': 2,
        'use-isnan': 2,
        'valid-typeof': 2,
        'no-useless-escape': 2,
        'require-atomic-updates': 'off'
    }
};

版本:

"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"eslint": "^7.18.0",

求解!谢谢!!!

阅读 5.6k
2 个回答

@typescript-eslint/no-unused-vars使用ts的规则

改成这样也不行:

type CallbackFn = (param: string, result: boolean) => void

const fn = (str: string, callback: CallbackFn) => {
    callback(str, true);
};

fn('test', () => {
    //
});

这样也是,同样报错:

interface CallbackFn {
    (param: string, result: boolean): void
}

const fn = (str: string, callback: CallbackFn) => {
    callback(str, true);
};

fn('test', () => {
    //
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进