2021-04-28 18:08:03 +02:00

36 lines
1.0 KiB
Plaintext

Parameter properties let you both create and initialize a member in one place, and omit an explicit member declaration and the assignment of the constructor parameter to the member. To use a parameter property, add an accessibility modifier or ``++readonly++``, or both in front of the constructor parameter.
----
constructor(readonly name: string, private age: number) { // creates 2 initialized members "name" and "age"
}
----
While this syntax is very concise it might be confusing for developers who are new to TypeScript.
Shared conventions allow teams to collaborate efficiently. This rule checks that either parameter properties are used everywhere or not at all.
== Noncompliant Code Example
Using the default parameter ``++forceParameterProperties++`` value ``++true++``:
----
class Person {
name: number;
constructor(name: string) {
this.name = name; // Noncompliant, parameter property can be used
}
}
----
== Compliant Solution
----
class Person {
constructor(public name: string) {
}
}
----