Skip to content

noParameterProperties

诊断类别:lint/style/noParameterProperties

¥Diagnostic Category: lint/style/noParameterProperties

自从:v1.0.0 来源:

¥Since: v1.0.0 Sources:

禁止在类构造函数中使用参数属性。

¥Disallow the use of 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 confuse those new to TypeScript as they are less explicit than other ways of declaring and initializing class members. Moreover, private class properties, starting with #, cannot be turned into “parameter properties”. This questions the future of this feature.

¥Examples

¥Invalid

class A {
constructor(readonly name: string) {}
}
code-block.ts:2:17 lint/style/noParameterProperties ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use a more explicit class property instead of a parameter property.

1 │ class A {
> 2 │ constructor(readonly name: string) {}
^^^^^^^^^^^^^^^^^^^^^
3 │ }
4 │

Parameter properties are less explicit than other ways of declaring and initializing class properties.

¥Valid

class A {
constructor(name: string) {}
}

¥Related links