tslint how to disable error “someVariable is declared but its value is never read ”

后端 未结 8 757
盖世英雄少女心
盖世英雄少女心 2021-02-02 05:05

I\'m using tslint, and got the error.

\'myVariable\' is declared but its value is never read.

I went to the website that documents the rules

相关标签:
8条回答
  • 2021-02-02 05:20

    There are two type of variables and you can do it in two ways

    1. argsIgnorePattern
    2. varsIgnorePattern

      • no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]
      • no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]

    Both these rule in eslint will ignore any function arguments and variables that starts with _ sign respectively

    0 讨论(0)
  • 2021-02-02 05:21

    Add this line just before the line which causes the error:

      /* tslint:disable:no-unused-variable */
    

    You will no longer receive the tslint error message.

    This is a better solution than turning off the error for you whole codebase in tslint.conf because then it wouldn't catch variables that really aren't used.

    0 讨论(0)
  • 2021-02-02 05:24

    Extend the tsconfig.json with dev.tsconfig.json

    And run the command tsc -p ./dev.tsconfig.json

    This will deisable the unused variable and unused parameter in development

    Inside dev.tsconfig.json:

    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "noUnusedLocals": false,
        "noUnusedParameters": false,
       }
    }
    
    0 讨论(0)
  • 2021-02-02 05:25

    I am using typescript": "2.9.1" with tslint": "^5.10.0.

    I was getting tons of error such as

    Property 'logger' is declared but its value is never read.
    

    Also, I observed that I was getting a warning when running ng-lint

    $> ng lint
    no-unused-variable is deprecated. Since TypeScript 2.9. Please use the built-in compiler checks instead.
    

    So, I removed the no-unused-variable rule fromt tslint.json - and that seems to solve the problem for me.

    0 讨论(0)
  • 2021-02-02 05:33

    Any parameter name starting with _ is exempt from the check. Use _myVariable instead of myvariable to remove this warning.

    0 讨论(0)
  • 2021-02-02 05:38

    First turn off noUnusedLocals in tsconfig.json:

    {
      "compilerOptions": {
        "noUnusedLocals": false,
      }
    }
    

    Then fix eslint rules in .eslintrc.js:

    module.exports = {
      rules: {
        'no-unused-vars': ['warn', { 'varsIgnorePattern': '^_' }],
        '@typescript-eslint/no-unused-vars': ['warn', { 'varsIgnorePattern': '^_' }],
      },
    };
    

    And If using @typescript-eslint/naming-convention rule should add leadingUnderscore: 'allow', For example, if you are using Airbnb config:

    '@typescript-eslint/naming-convention': [
      'error',
      {
        selector: 'variable',
        format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
        leadingUnderscore: 'allow',
      },
      {
        selector: 'function',
        format: ['camelCase', 'PascalCase'],
      },
      {
        selector: 'typeLike',
        format: ['PascalCase'],
      },
    ],
    
    0 讨论(0)
提交回复
热议问题