首页 > 解决方案 > 我可以关闭用于在 lambdas 中解构的 eslint tyfedef 规则吗

问题描述

我想知道是否可以只为 lambdas 中的数组或对象解构关闭 typedef 规则?

getPersonsNames(): string[] {
    type Person = { name: string; age: number };
    const persons: Person[] = [
        { name: `Jan Kowalski`, age: 12 },
        { name: `Justyna Kowalczyk`, age: 22 }
    ];
    return persons.map(({ name }) => name); // ESLint: Expected a type annotation.(@typescript-eslint/typedef)
}

一般来说,我想使用 typedfees 进行解构,但在这种情况下我不想这样做。有没有办法排除这些情况?


我尝试添加'arrow-parameter': false,arrowParameter: false如您在上面看到的),@typescript-eslint/typedef但它根本没有帮助。

我使用的这条规则的文档:@typescript-eslint/typedef

要重现的文件

.eslintrc.js配置文件:

module.exports = {
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: './tsconfig.json',
        createDefaultProgram: true,
        ecmaVersion: 2020,
        sourceType: 'module',
    },
    extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended',
    ],
    rules: {
        '@typescript-eslint/typedef': [
            'error',
            {
                'arrowParameter': false,
                'propertyDeclaration': true,
                'parameter': true,
                'memberVariableDeclaration': true,
                'callSignature': true,
                'variableDeclaration': true,
                'arrayDestructuring': true,
                'objectDestructuring': true
            }
        ],
    },
}

.gitignore

node_modules

index.ts

function getPersonsNames(): string[] {
    type Person = { name: string; age: number };
    const persons: Person[] = [
        { name: `Jan Kowalski`, age: 12 },
        { name: `Justyna Kowalczyk`, age: 22 }
    ];
    return persons.map(({ name }) => name); // ESLint: Expected a type annotation.(@typescript-eslint/typedef)
}

getPersonsNames();

package.json

{
  "name": "typedef-in-destructuring-lambdas",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint . --ext .ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.3.0",
    "@typescript-eslint/parser": "^4.3.0",
    "eslint": "^7.10.0",
    "typescript": "^4.0.3"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "target": "ES2017",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "noEmit": true,
        "noEmitHelpers": true,
        "importHelpers": true,
        "strictNullChecks": false,
        "skipLibCheck": true,
        "lib": [
            "dom",
            "es6",
            "es2019"
        ]
    }
}

标签: typescripteslintdestructuringtypescript-eslint

解决方案


该规则不支持这一点——它将所有解构都视为相同。
请注意,不会将更多可定制性添加到规则中,因为它不应该在大多数代码库中使用。

使用它并添加不必要的类型注释是一种反模式,并且会对您的代码库产生负面影响。


这条规则并不是真的打算在代码库中日常使用,它旨在帮助您迁移代码库,以便您可以打开noImplicitAny编译器选项。

到处都是不必要的类型注释对您的代码库不利。每个都会产生维护成本(您必须手动更新它们以使其保持同步),并且每个还会减慢编译速度,因为 TypeScript 必须花时间来验证注释是否正确。

作为 的维护者@typescript-eslint,我强烈建议不要使用该typedef规则。


推荐阅读