问题描述
我想知道是否可以仅针对lambda中的数组或对象销毁关闭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
要复制的文件
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: 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"
]
}
}
解决方法
此规则不支持此规则-它将所有销毁处理视为相同。
请注意,不会在规则中添加更多可自定义性,因为大多数代码库中不应使用它。
使用它并添加不必要的类型注释是一种反模式,会对您的代码库产生负面影响。
此规则并非真正打算在代码库中日常使用,而是旨在帮助您迁移代码库,以便可以打开noImplicitAny
编译器选项。
到处都是不必要的类型注释,这对您的代码库不利。每一个都会产生维护成本(您必须手动更新它们以使其保持同步),并且每一个也会减慢编译速度,因为TypeScript必须花时间来验证注释的正确性。 / p>
作为@typescript-eslint
的维护者,我 坚强 使用typedef
规则建议反对。