59 lines
1.4 KiB
Plaintext
59 lines
1.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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++``:
|
|
|
|
[source,javascript]
|
|
----
|
|
class Person {
|
|
name: number;
|
|
constructor(name: string) {
|
|
this.name = name; // Noncompliant, parameter property can be used
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
class Person {
|
|
constructor(public name: string) {
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::parameters.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|