有没有办法忽略 "Missing return type on function" 的 Angular 生命周期方法?
Is there a way to ignore "Missing return type on function" for Angular lifecycle methods?
我们正在使用 Angular,我正在尝试从 tslint 切换到 eslint。
抛出大约 600 个警告的规则是 Missing return type on function
来自 eslint@typescript-eslint/explicit-module-boundary-types
.
我明白我应该为我的函数使用 return 类型以及如何使用。 Angular 中几乎没有生命周期方法。我应该从 ngOnInit
return 做什么?
所以问题是:我如何将此规则用于每个函数而不是 ngOnInit
、ngOnDestroy
等?
ngOnInit(): void { // meh
...
}
ngOnInit() { // yay
...
}
以防万一它是相关的,我的 .eslintrc.json:
extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
我没有测试它,但看起来该规则接受一个 allowedNames
选项,您可以在其中传递一组不被检查的方法名称。
请注意,这还将禁用检查方法参数的类型。
解决这个问题的方法有:
- 在每一行之前添加错误注释,如下所示,以忽略所有错误:
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
- 全局忽略此类错误:
"extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"plugins": ["unused-imports"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off"
}
根据@Ibsn 的回答我可以想出这个配置:
"rules": {
"@typescript-eslint/explicit-module-boundary-types": [
"warn",
{
"allowedNames": ["ngOnInit", "ngOnDestroy", "ngAfterViewInit", "ngOnChanges"]
}
]
}
我们正在使用 Angular,我正在尝试从 tslint 切换到 eslint。
抛出大约 600 个警告的规则是 Missing return type on function
来自 eslint@typescript-eslint/explicit-module-boundary-types
.
我明白我应该为我的函数使用 return 类型以及如何使用。 Angular 中几乎没有生命周期方法。我应该从 ngOnInit
return 做什么?
所以问题是:我如何将此规则用于每个函数而不是 ngOnInit
、ngOnDestroy
等?
ngOnInit(): void { // meh
...
}
ngOnInit() { // yay
...
}
以防万一它是相关的,我的 .eslintrc.json:
extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
我没有测试它,但看起来该规则接受一个 allowedNames
选项,您可以在其中传递一组不被检查的方法名称。
请注意,这还将禁用检查方法参数的类型。
解决这个问题的方法有:
- 在每一行之前添加错误注释,如下所示,以忽略所有错误:
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
- 全局忽略此类错误:
"extends": [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"plugins": ["unused-imports"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off"
}
根据@Ibsn 的回答我可以想出这个配置:
"rules": {
"@typescript-eslint/explicit-module-boundary-types": [
"warn",
{
"allowedNames": ["ngOnInit", "ngOnDestroy", "ngAfterViewInit", "ngOnChanges"]
}
]
}