![](/img/trans.png)
[英]Can not get rule explicit-function-return-type to turn off in eslint for typescript linting
[英]Can I turn off eslint tyfedef rule for 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)
}
一般來說,我想使用 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
規則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.