简体   繁体   English

我可以关闭用于在 lambdas 中解构的 eslint tyfedef 规则吗

[英]Can I turn off eslint tyfedef rule for destructuring in lambdas

I am wondering is it possible to turn off typedef rule only for array or object destructuring in lambdas ?我想知道是否可以仅针对 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)
}

In general, I want to use typedfees for destructuring but in those kinds of cases I do not want to.一般来说,我想使用 typedfees 进行解构,但在这种情况下我不想。 Is there a way to exclude those cases ?有没有办法排除这些情况?


I tried to add 'arrow-parameter': false, (and arrowParameter: false as you can see above) to @typescript-eslint/typedef but it didn't help at all.我试图将'arrow-parameter': false, (和arrowParameter: false如你所见)添加到@typescript-eslint/typedef但它根本没有帮助。

Documentation of this rule which I used: @typescript-eslint/typedef我使用的这条规则的文档: @typescript-eslint/typedef

Files to reproduce要复制的文件

.eslintrc.js config file: .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 : .gitignore

node_modules

index.ts : 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 : 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 : 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"
        ]
    }
}

The rule does not support this - it treats all destructuring as the same.该规则不支持这一点——它将所有解构视为相同。
Note that more customisability won't be added to the rule because it shouldn't be used in most codebases.请注意,更多的可定制性不会添加到规则中,因为它不应在大多数代码库中使用。

Using it, and adding unnecessary type annotations is an anti-pattern, and has a negative effect on your codebase.使用它并添加不必要的类型注释是一种反模式,会对您的代码库产生负面影响。


This rule isn't really intended to be used day-to-day in a codebase, it's intended to help you migrate your codebase so you can turn on the noImplicitAny compiler option.此规则并不是真的要在代码库中日常使用,它旨在帮助您迁移代码库,以便您可以打开noImplicitAny编译器选项。

Unnecessary type annotations everywhere is bad for your codebase.到处都是不必要的类型注释对您的代码库不利。 Each one incurs a maintenance cost (you have to manually update them to keep them in sync), and each one also slows down compilation , because TypeScript has to take time to validate that the annotation is correct.每个都会产生维护成本(您必须手动更新它们以保持同步),并且每个都会减慢编译速度,因为 TypeScript 必须花时间来验证注释是否正确。

As the maintainer of @typescript-eslint , I strongly advise against using the typedef rule.作为@typescript-eslint ,我强烈建议不要使用typedef规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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