What is the best way of making a TypeScript class final, and ensure that all its invariants are guaranteed, even during runtime (when it might be accessed by arbitrary JavaScript code)?
A simple example of what I want to do is the following MutableInt
class:
export class MutableInt {
static {
Object.setPrototypeOf(MutableInt.prototype, null);
Object.freeze(MutableInt.prototype);
Object.freeze(MutableInt);
}
#value : int
constructor (value : int = 0) {
if (new.target !== MutableInt) throw new Error("MutableInt is final");
if (!Number.isInteger(value)) throw new Error(`Integer expected, found: ${value}`);
this.#value = value;
Object.freeze(this);
}
get value(): number {
return this.#value;
}
set value(v) {
if (!Number.isInteger(v)) throw new Error(`Integer expected, found: ${v}`);
this.#value = v;
}
}
Is MutableInt
properly locked down? Or is there something wrong or missing with this approach?