parameter-properties
Require or disallow parameter properties in class constructors.
TypeScript 包含 "参数属性" 简写,用于在一个位置声明类构造函数参数和类属性。 参数属性可能会让 TypeScript 新手感到困惑,因为它们比声明和初始化类成员的其他方法不太明确。
英:TypeScript includes a "parameter properties" shorthand for declaring a class constructor parameter and class property in one location. Parameter properties can be confusing to those new to TypeScript as they are less explicit than other ways of declaring and initializing class members.
可以将此规则配置为始终禁止使用参数属性或在可能的情况下强制使用它们。
英:This rule can be configured to always disallow the use of parameter properties or enforce their usage when possible.
module.exports = {
"rules": {
"@typescript-eslint/parameter-properties": "error"
}
};
选项
该规则在默认状态下不需要任何参数,并且完全禁止使用参数属性。 它可能需要一个包含以下任一或两者的选项对象:
英:This rule, in its default state, does not require any argument and would completely disallow the use of parameter properties. It may take an options object containing either or both of:
"allow"
: 允许忽略某些类型的属性"prefer"
:"class-property"
(默认)或"parameter-property"
allow
如果你想忽略某些类型的属性,则可以将包含 "allow"
的对象作为以下任意选项的数组传递:
英:If you would like to ignore certain kinds of properties then you may pass an object containing "allow"
as an array of any of the following options:
allow
,包含一个或多个允许的修饰符的数组。 有效值为:readonly
,允许 readonly 参数属性。private
,允许 private 参数属性。protected
,允许 protected 参数属性。public
,允许 public 参数属性。private readonly
,允许 私有只读 参数属性。protected readonly
,允许 受保护的只读 参数属性。public readonly
,允许 公共只读 参数属性。
例如,要忽略 public
属性:
英:For example, to ignore public
properties:
{
"@typescript-eslint/parameter-properties": [
true,
{
"allow": ["public"]
}
]
}
prefer
默认情况下,规则优先选择类属性 ("class-property"
)。
你可以使用 ("parameter-property"
) 将其切换为首选参数属性。
英:By default, the rule prefers class property ("class-property"
).
You can switch it to instead preferring parameter property with ("parameter-property"
).
在 "parameter-property"
模式下,规则将在以下情况下触发报告:
英:In "parameter-property"
mode, the rule will issue a report when:
- 类属性和构造函数参数具有相同的名称和类型
- 构造函数参数在构造函数的开头被赋值给类属性
default
此规则的代码示例(根本没有选项):
英:Examples of code for this rule with no options at all:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
Open in Playgroundreadonly
{ "allow": ["readonly"] }
选项的代码示例:
英:Examples of code for the { "allow": ["readonly"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(readonly name: string) {}
}
Open in Playgroundprivate
{ "allow": ["private"] }
选项的代码示例:
英:Examples of code for the { "allow": ["private"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(private name: string) {}
}
Open in Playgroundprotected
{ "allow": ["protected"] }
选项的代码示例:
英:Examples of code for the { "allow": ["protected"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
Open in Playgroundpublic
{ "allow": ["public"] }
选项的代码示例:
英:Examples of code for the { "allow": ["public"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(public name: string) {}
}
Open in Playground私有只读
{ "allow": ["private readonly"] }
选项的代码示例:
英:Examples of code for the { "allow": ["private readonly"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
Open in Playground受保护的只读
{ "allow": ["protected readonly"] }
选项的代码示例:
英:Examples of code for the { "allow": ["protected readonly"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
Open in Playground公共只读
{ "allow": ["public readonly"] }
选项的代码示例:
英:Examples of code for the { "allow": ["public readonly"] }
options:
- ❌ 不正确
- ✅ 正确
class Foo {
constructor(readonly name: string) {}
}
class Foo {
constructor(private name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
class Foo {
constructor(public name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
Open in Playgroundclass Foo {
constructor(name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
Open in Playground"parameter-property"
{ "prefer": "parameter-property" }
选项的代码示例:
英:Examples of code for the { "prefer": "parameter-property" }
option:
- ❌ 不正确
- ✅ 正确
class Foo {
private name: string;
constructor(name: string) {
this.name = name;
}
}
class Foo {
public readonly name: string;
constructor(name: string) {
this.name = name;
}
}
class Foo {
constructor(name: string) {
this.name = name;
}
name: string;
}
Open in Playgroundclass Foo {
private differentName: string;
constructor(name: string) {
this.differentName = name;
}
}
class Foo {
private differentType: number | undefined;
constructor(differentType: number) {
this.differentType = differentType;
}
}
class Foo {
protected logicInConstructor: string;
constructor(logicInConstructor: string) {
console.log('Hello, world!');
this.logicInConstructor = logicInConstructor;
}
}
Open in Playground何时不使用它
如果你不关心类中使用构造函数中的参数属性的哪种样式,那么你将不需要此规则。
英:If you don't care about which style of parameter properties in constructors is used in your classes, then you will not need this rule.
但是,请记住,不一致的风格可能会损害项目的可读性。 我们建议为此规则选择一个最适合你的项目的选项。
英:However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.
选项
该规则接受以下选项
type Modifier =
| 'private readonly'
| 'private'
| 'protected readonly'
| 'protected'
| 'public readonly'
| 'public'
| 'readonly';
type Options = [
{
allow?: Modifier[];
prefer?: 'class-property' | 'parameter-property';
},
];
const defaultOptions: Options = [{ allow: [], prefer: 'class-property' }];