Eslint,里面没有带有 super() 的无用构造函数?
Eslint, no useless constructor with super() inside?
代码:
constructor(cdr: ChangeDetectorRef, inj: Injector) {
super(cdr, inj);
}
eslint:
{
...
overrides: {
...
rules: {
...
"@typescript-eslint/no-useless-constructor": "error",
...
}
}
}
这条规则告诉我要删除无用的constructor
,但我必须在里面调用super()。
如何在不禁用或删除该规则的情况下绕过它?
只需删除构造函数。如果您没有进一步使用,则无需定义它。对于您的扩展 class,它与空构造函数相同。它没用,因为它由 JavaScript/TypeScript 自动处理。参见 docs。
If you don't provide your own constructor, then a default constructor will be supplied for you. ...
If your class is a derived class, the default constructor calls the parent constructor, passing along any arguments that were provided.
所以是的,派生的 classes 的构造函数必须包含超级调用。
但是你不一定要定义一个空的构造函数或者只在里面定义 super
(derived classes)。除非你想添加更多或不同的签名。
如果您仍想保留它,只需 remove/disable linter 规则即可。
有一个注意事项要知道:
This lint rule will report on constructors whose sole purpose is to change visibility of a parent constructor. -- TS Docs also see ES Docs
Angular 可能需要这个带有 super 调用的“无用”构造函数,例如Injectable 和 Constructor(派生 classes)。这可能会导致错误:This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
在这种情况下,必须禁用 eslint 规则。
代码:
constructor(cdr: ChangeDetectorRef, inj: Injector) {
super(cdr, inj);
}
eslint:
{
...
overrides: {
...
rules: {
...
"@typescript-eslint/no-useless-constructor": "error",
...
}
}
}
这条规则告诉我要删除无用的constructor
,但我必须在里面调用super()。
如何在不禁用或删除该规则的情况下绕过它?
只需删除构造函数。如果您没有进一步使用,则无需定义它。对于您的扩展 class,它与空构造函数相同。它没用,因为它由 JavaScript/TypeScript 自动处理。参见 docs。
If you don't provide your own constructor, then a default constructor will be supplied for you. ... If your class is a derived class, the default constructor calls the parent constructor, passing along any arguments that were provided.
所以是的,派生的 classes 的构造函数必须包含超级调用。
但是你不一定要定义一个空的构造函数或者只在里面定义 super
(derived classes)。除非你想添加更多或不同的签名。
如果您仍想保留它,只需 remove/disable linter 规则即可。
有一个注意事项要知道:
This lint rule will report on constructors whose sole purpose is to change visibility of a parent constructor. -- TS Docs also see ES Docs
Angular 可能需要这个带有 super 调用的“无用”构造函数,例如Injectable 和 Constructor(派生 classes)。这可能会导致错误:This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
在这种情况下,必须禁用 eslint 规则。