我的项目带有打字稿+ eslint堆栈。
规则@typescript-eslint/typedef
的问题:
当我尝试使用命令eslint --fix
时,类型注释已删除:
示例:
formCtrlName: string = '';
在命令eslint --fix
之后,我将得到下一个代码:
formCtrlName = '';
和错误:
26:3 error expected formCtrlName to have a type annotation @typescript-eslint/typedef
陪同的规则:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
},
plugins: ['@typescript-eslint',
'eslint-plugin-tsdoc',
'simple-import-sort',
'unicorn',
'jsdoc'
],
// camelcase, indent, no-array-constructor, and no-unused-vars are all
// busted when using TypeScript at the moment. When you use this plugin, you're
// forced to turn off the base rules from ESLint and turn on the TypeScript-friendly
// variant that comes with @typescript-eslint/eslint-plugin.
rules: {
// tsdoc rules
'tsdoc/syntax': 'error',
'jsdoc/require-param-description': 2,
'jsdoc/require-param-name': 2,
'jsdoc/require-param': 2,
'jsdoc/check-alignment': 2,
'jsdoc/check-indentation': 2,
'jsdoc/check-param-names': 2,
'jsdoc/check-values': 2,
'jsdoc/no-types': 2,
// using a single import statement per module
'no-duplicate-imports': ['error', { 'includeExports': true }],
// avoid using methods on console
'no-console': ['error', { allow: ['warn', 'error'] }],
// camelcase interference fix.
camelcase: 'off',
'@typescript-eslint/camelcase': [2, { properties: 'never' }],
// indent interference fix.
indent: 'off',
'@typescript-eslint/indent': ['error', 2, { SwitchCase: 1 }],
// no-array-constructor interference fix.
'no-array-constructor': 'off',
'@typescript-eslint/no-array-constructor': 'error',
// no-unused-vars interference fix.
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-empty-interface': 'error',
'@typescript-eslint/explicit-function-return-type': [
'error',
{
allowExpressions: true,
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
},
],
'@typescript-eslint/no-explicit-any': ['warn', { 'ignoreRestArgs': false }],
// note you must disable the base rule as it can report incorrect errors
"no-magic-numbers": "off",
// for future release
// '@typescript-eslint/no-extra-non-null-assertion': ['warn'],
'@typescript-eslint/no-magic-numbers': ['error', {
'ignoreNumericLiteralTypes': true,
'ignoreReadonlyClassProperties': true,
'ignoreEnums': true,
'ignoreArrayIndexes': true,
'ignore': [0, 1, 2, 10, 100, 1000]
}],
'@typescript-eslint/no-unnecessary-condition': ['error'],
// note you must disable the base rule as it can report incorrect errors
'no-unused-expressions': 'off',
'@typescript-eslint/no-unused-expressions': ['error'],
'@typescript-eslint/no-use-before-define': ['error', { 'functions': true, 'classes': true }],
'@typescript-eslint/restrict-plus-operands': 'error',
// note you must disable the base rule as it can report incorrect errors
'semi': 'off',
'@typescript-eslint/semi': ['error'],
'@typescript-eslint/strict-boolean-expressions': ['error'],
'@typescript-eslint/type-annotation-spacing': ['error'],
'@typescript-eslint/typedef': [
'error',
{
'arrowParameter': false,
'variableDeclaration': true,
}
],
'typescript-eslint/no-inferrable-types': 'off',
// Enforces naming conventions for everything across a codebase
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "default",
"format": ["camelCase"]
},
{
"selector": "variable",
"format": ["camelCase", "UPPER_CASE"]
},
{
"selector": "parameter",
"format": ["camelCase"],
"leadingUnderscore": "allow"
},
{
"selector": "variable",
"types": ["boolean"],
"format": ["camelCase"],
"prefix": ["is", "should", "has", "can", "did", "will"]
},
{
"selector": "typeLike",
"format": ["PascalCase"]
}
],
// disable eslint sort imports
'sort-imports': 'off',
'import/order': 'off',
// checks all import declarations and verifies that all imports are first sorted by the used member syntax
// and then alphabetically by the first member or alias name
'simple-import-sort/sort': 'error',
// check file names for kebabCase
'unicorn/filename-case': ['error', { "case": "kebabCase" }],
},
};
我该如何解决?
当我尝试使用规则@typescript-eslint/no-inferrable-types
时遇到冲突,规则@typescript-eslint/typedef
也是如此。
答案 0 :(得分:0)
你可以试试
// eslint-disable-next-line @typescript-eslint/typedef
在有问题的代码之前。