【发布时间】: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