24 lines
780 B
Plaintext
24 lines
780 B
Plaintext
``++readonly++`` properties can only be assigned in a class constructor or at the point of declaration. If a class has a property that's not marked ``++readonly++`` but is only set in the constructor, it could cause confusion about the property's intended use. To avoid confusion, such properties should be marked ``++readonly++`` to make their intended use explicit, and to prevent future maintainers from inadvertently changing their use.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
class Person {
|
|
private _birthYear: number; // Noncompliant
|
|
constructor(birthYear: number) {
|
|
this._birthYear = birthYear;
|
|
}
|
|
}
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
class Person {
|
|
private readonly _birthYear: number;
|
|
constructor(birthYear: number) {
|
|
this._birthYear = birthYear;
|
|
}
|
|
}
|
|
----
|