25 lines
807 B
Plaintext
25 lines
807 B
Plaintext
== Why is this an issue?
|
|
|
|
Public ``++static++`` fields in TypeScript should be declared as ``++readonly++`` to prevent them from being modified after their initial assignment. This is a good practice because it makes the code safer by preventing accidental changes to these fields, which could lead to bugs that are hard to detect.
|
|
|
|
[source,typescript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
class MyClass {
|
|
static myField = 42; // Noncompliant
|
|
}
|
|
----
|
|
|
|
To fix this, declare your static field with the ``++readonly++`` qualifier.
|
|
|
|
[source,typescript,diff-id=1,diff-type=compliant]
|
|
----
|
|
class MyClass {
|
|
static readonly myField = 42;
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
== Documentation
|
|
|
|
* TypeScript Documentation - https://www.typescriptlang.org/docs/handbook/2/objects.html#readonly-properties[readonly properties]
|