24 lines
780 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
``++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.
2020-06-30 12:48:07 +02:00
== 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;
}
}
----