参数属性
要求或禁止在类构造函数中使用参数属性。
TypeScript 包含一个“参数属性”简写,用于在一个位置声明类构造函数参数和类属性。参数属性对于 TypeScript 的新手来说可能令人困惑,因为它们不像其他声明和初始化类成员的方式那样明确。
此规则可以配置为始终禁止使用参数属性或在可能的情况下强制使用它们。
module.exports = {
"rules": {
"@typescript-eslint/parameter-properties": "error"
}
};
在游乐场中尝试此规则 ↗
选项
此规则接受以下选项
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' }];
此规则在其默认状态下不需要任何参数,并且会完全禁止使用参数属性。它可以接受一个包含以下任一或两者的选项对象:
"allow"
:允许忽略某些类型的属性"prefer"
:"class-property"
(默认) 或"parameter-property"
allow
如果您想忽略某些类型的属性,则可以传递一个包含 "allow"
作为以下任何选项数组的对象
allow
,一个包含一个或多个允许修饰符的数组。有效值为readonly
,允许 readonly 参数属性。private
,允许 private 参数属性。protected
,允许 protected 参数属性。public
,允许 public 参数属性。private readonly
,允许 private readonly 参数属性。protected readonly
,允许 protected readonly 参数属性。public readonly
,允许 public readonly 参数属性。
例如,要忽略 public
属性
{
"@typescript-eslint/parameter-properties": [
true,
{
"allow": ["public"]
}
]
}
prefer
默认情况下,该规则更喜欢类属性 ("class-property"
)。您可以将其切换为改为更喜欢参数属性 ("parameter-property"
)。
在 "parameter-property"
模式下,该规则将在以下情况下发出报告:
- 类属性和构造函数参数具有相同的名称和类型
- 构造函数参数在构造函数开始时被分配给类属性
default
此规则没有任何选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
在 Playground 中打开只读
{ "allow": ["readonly"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(readonly name: string) {}
}
在 Playground 中打开私有
{ "allow": ["private"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(private name: string) {}
}
在 Playground 中打开受保护
{ "allow": ["protected"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(protected name: string) {}
}
在 Playground 中打开公共
{ "allow": ["public"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(public name: string) {}
}
在 Playground 中打开私有只读
{ "allow": ["private readonly"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(private readonly name: string) {}
}
在 Playground 中打开受保护只读
{ "allow": ["protected readonly"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(protected readonly name: string) {}
}
在 Playground 中打开公共只读
{ "allow": ["public readonly"] }
选项的代码示例
- ❌ 错误
- ✅ 正确
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) {}
}
在 Playground 中打开class Foo {
constructor(name: string) {}
}
class Foo {
constructor(public readonly name: string) {}
}
在 Playground 中打开"parameter-property"
{ "prefer": "parameter-property" }
选项的代码示例
- ❌ 错误
- ✅ 正确
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;
}
在 Playground 中打开class 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;
}
}
在 Playground 中打开何时不使用它
如果您不在乎在类中使用哪种构造函数参数属性样式,那么您将不需要此规则。
但是,请记住,不一致的样式会损害项目的可读性。我们建议为该规则选择一个最适合您的项目的选项。