Tymczasem można to rozwiązać za pomocą dekoratora w połączeniu z, Object.freezelub Object.defineProperty, używam tego, jest to trochę ładniejsze niż używanie ton getterów. Możesz skopiować / wkleić ten bezpośrednio TS Playground, aby zobaczyć go w akcji. - Istnieją dwie opcje
Ustaw poszczególne pola jako „końcowe”
Poniższy dekorator konwertuje zarówno statyczne, jak i niestatyczne pola z adnotacjami na „właściwości tylko gettera”.
Uwaga : Jeśli adnotacja ma zmienna instancji bez wartości początkowej @final, to pierwsza przypisana wartość (bez względu na to, kiedy) będzie ostateczna.
// example
class MyClass {
@final
public finalProp: string = "You shall not change me!";
@final
public static FINAL_FIELD: number = 75;
public static NON_FINAL: string = "I am not final."
}
var myInstance: MyClass = new MyClass();
myInstance.finalProp = "Was I changed?";
MyClass.FINAL_FIELD = 123;
MyClass.NON_FINAL = "I was changed.";
console.log(myInstance.finalProp); // => You shall not change me!
console.log(MyClass.FINAL_FIELD); // => 75
console.log(MyClass.NON_FINAL); // => I was changed.
Dekorator: upewnij się, że umieściłeś to w swoim kodzie!
/**
* Turns static and non-static fields into getter-only, and therefor renders them "final".
* To use simply annotate the static or non-static field with: @final
*/
function final(target: any, propertyKey: string) {
const value: any = target[propertyKey];
// if it currently has no value, then wait for the first setter-call
// usually the case with non-static fields
if (!value) {
Object.defineProperty(target, propertyKey, {
set: function (value: any) {
Object.defineProperty(this, propertyKey, {
get: function () {
return value;
},
enumerable: true,
configurable: false
});
},
enumerable: true,
configurable: true
});
} else { // else, set it immediatly
Object.defineProperty(target, propertyKey, {
get: function () {
return value;
},
enumerable: true
});
}
}
Alternatywą dla powyższego dekoratora byłaby też jego ścisła wersja, która rzucałaby nawet Błąd, gdy ktoś próbował przypisać jakąś wartość do pola z "use strict";ustawionym. (Jest to jednak tylko część statyczna)
/**
* Turns static fields into getter-only, and therefor renders them "final".
* Also throws an error in strict mode if the value is tried to be touched.
* To use simply annotate the static field with: @strictFinal
*/
function strictFinal(target: any, propertyKey: string) {
Object.defineProperty(target, propertyKey, {
value: target[propertyKey],
writable: false,
enumerable: true
});
}
Niech każde pole statyczne będzie „ostateczne”
Możliwe wady: Będzie to działać tylko dla WSZYSTKICH statyki tej klasy lub dla żadnej, ale nie może być zastosowane do konkretnej statyki.
/**
* Freezes the annotated class, making every static 'final'.
* Usage:
* @StaticsFinal
* class MyClass {
* public static SOME_STATIC: string = "SOME_STATIC";
* //...
* }
*/
function StaticsFinal(target: any) {
Object.freeze(target);
}
// Usage here
@StaticsFinal
class FreezeMe {
public static FROZEN_STATIC: string = "I am frozen";
}
class EditMyStuff {
public static NON_FROZEN_STATIC: string = "I am frozen";
}
// Test here
FreezeMe.FROZEN_STATIC = "I am not frozen.";
EditMyStuff.NON_FROZEN_STATIC = "I am not frozen.";
console.log(FreezeMe.FROZEN_STATIC); // => "I am frozen."
console.log(EditMyStuff.NON_FROZEN_STATIC); // => "I am not frozen."