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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/parameter-properties": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/parameter-properties": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Modifier =
| 'private readonly'
| 'private'
| 'protected readonly'
| 'protected'
| 'public readonly'
| 'public'
| 'readonly';
type Options = [
{
/** Whether to allow certain kinds of properties to be ignored. */
allow?: Modifier[];
/** Whether to prefer class properties or parameter properties. */
prefer?:
| 'parameter-property'
/** Whether to prefer class properties or parameter properties. */
| 'class-property';
},
];
const defaultOptions: Options = [{ allow: [], prefer: 'class-property' }];
¥Options
该规则在默认状态下不需要任何参数,并且完全禁止使用参数属性。它可能需要一个包含以下任一或两者的选项对象:
¥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"
:允许忽略某些类型的属性¥
"allow"
: allowing certain kinds of properties to be ignored -
"prefer"
:"class-property"
(默认)或"parameter-property"
¥
"prefer"
: either"class-property"
(default) or"parameter-property"
allow
Whether to allow certain kinds of properties to be ignored. Default: []
.
如果你想要忽略某些类型的属性,那么你可以将包含 "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
,包含一个或多个允许的修饰符的数组。有效值为:¥
allow
, an array containing one or more of the allowed modifiers. Valid values are:-
readonly
,允许只读参数属性。¥
readonly
, allows readonly parameter properties. -
private
,允许私有参数属性。¥
private
, allows private parameter properties. -
protected
,允许受保护的参数属性。¥
protected
, allows protected parameter properties. -
public
,允许公共参数属性。¥
public
, allows public parameter properties. -
private readonly
,允许私有只读参数属性。¥
private readonly
, allows private readonly parameter properties. -
protected readonly
,允许受保护的只读参数属性。¥
protected readonly
, allows protected readonly parameter properties. -
public readonly
,允许公共只读参数属性。¥
public readonly
, allows public readonly parameter properties.
-
例如,要忽略 public
属性:
¥For example, to ignore public
properties:
{
"@typescript-eslint/parameter-properties": [
true,
{
"allow": ["public"]
}
]
}
prefer
Whether to prefer class properties or parameter properties. Default: "class-property"
.
默认情况下,规则优先考虑类属性。你可以将其切换为使用 ("parameter-property"
) 的参数属性。
¥By default, the rule prefers class properties.
You can switch it to instead preferring parameter properties with ("parameter-property"
).
在 "parameter-property"
模式下,规则将在以下情况下发出报告:
¥In "parameter-property"
mode, the rule will issue a report when:
-
类属性和构造函数参数具有相同的名称和类型
¥A class property and constructor parameter have the same name and type
-
构造函数参数在构造函数的开头被赋值给类属性
¥The constructor parameter is assigned to the class property at the beginning of the constructor
default
没有任何选项的此规则的代码示例:
¥Examples of code for this rule with no options at all:
- ❌ Incorrect
- ✅ Correct
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:
- ❌ Incorrect
- ✅ Correct
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:
- ❌ Incorrect
- ✅ Correct
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:
- ❌ Incorrect
- ✅ Correct
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:
- ❌ Incorrect
- ✅ Correct
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私有只读
¥private readonly
{ "allow": ["private readonly"] }
选项的代码示例:
¥Examples of code for the { "allow": ["private readonly"] }
options:
- ❌ Incorrect
- ✅ Correct
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受保护的只读
¥protected readonly
{ "allow": ["protected readonly"] }
选项的代码示例:
¥Examples of code for the { "allow": ["protected readonly"] }
options:
- ❌ Incorrect
- ✅ Correct
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公共只读
¥public readonly
{ "allow": ["public readonly"] }
选项的代码示例:
¥Examples of code for the { "allow": ["public readonly"] }
options:
- ❌ Incorrect
- ✅ Correct
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:
- ❌ Incorrect
- ✅ Correct
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何时不使用它
¥When Not To Use It
如果你不关心类中使用构造函数中的参数属性的哪种样式,那么你将不需要此规则。
¥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.
'## 资源'