typescript - 我可以关闭用于在 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"
]
}
}
解决方案
该规则不支持这一点——它将所有解构都视为相同。
请注意,不会将更多可定制性添加到规则中,因为它不应该在大多数代码库中使用。
使用它并添加不必要的类型注释是一种反模式,并且会对您的代码库产生负面影响。
这条规则并不是真的打算在代码库中日常使用,它旨在帮助您迁移代码库,以便您可以打开noImplicitAny
编译器选项。
到处都是不必要的类型注释对您的代码库不利。每个都会产生维护成本(您必须手动更新它们以使其保持同步),并且每个还会减慢编译速度,因为 TypeScript 必须花时间来验证注释是否正确。
作为 的维护者@typescript-eslint
,我强烈建议不要使用该typedef
规则。
推荐阅读
- reactjs - 安装时出错:- npm install -g create-react-app
- c# - SetWindowPlacement function does not setting the console window size properly
- javascript - 如何将每行元素转换为数组,所以我只能选择一个元素,如 data[2]
- python - Regex with negation for extracting web link
- spring-boot - 在 Spring Boot 中使用注释以正确格式放置数据
- java - Java中的x 509证书和X509CertificateHolder有什么区别?
- javascript - AJAX 调用 PHP 函数后禁用 AJAX/Javascript 弹出窗口
- c++ - C++ 模板类给出了“错误:非模板 X 用作模板”
- java - Citrus Simulator 在接收序列化的 xml 数据时抛出 Nullpointer 异常
- c++ - 对于 C++,有 std::strchr() 的任何现代替代品吗?