【问题标题】:Can I turn off eslint tyfedef rule for destructuring in lambdas我可以关闭用于在 lambdas 中解构的 eslint tyfedef 规则吗
【发布时间】:2020-10-19 04:59:28
【问题描述】:

我想知道是否可以只为 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"
        ]
    }
}

【问题讨论】:

  • 为什么要强制使用不必要的类型注解?
  • @T.J.Crowder 这是这个项目和团队工作流程中的要求:/
  • 这很不幸。 :-)
  • 不幸的是:')
  • 我已将这些文件的内容复制到问题中。整个问题(包括任何必要的代码)必须问题中,而不仅仅是链接。三个原因:人们不应该去场外帮助你;某些网站被某些用户屏蔽;和链接腐烂,使问题及其答案对未来的人们毫无用处。

标签: typescript eslint destructuring typescript-eslint


【解决方案1】:

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

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


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

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

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

【讨论】:

  • 说得很好。此外,不必要的类型注释可以充当隐藏的类型断言(即let xs: any[] = [{}]; xs.map((x: string) => x.toUpperCase()) // terrible!
  • 嗯,我不知道这会影响编译过程(很好)。很好的答案。谢谢老哥
猜你喜欢
  • 2019-02-28
  • 2020-03-25
  • 2023-01-05
  • 2020-01-26
  • 2019-08-30
  • 2021-05-21
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多