¿Cuál es la mejor manera de hacer que una clase de TypeScript sea final y asegurarse de que todas sus invariantes estén garantizadas, incluso durante el tiempo de ejecución (cuando se puede acceder a ella mediante un código JavaScript arbitrario)?
Un ejemplo simple de lo que quiero hacer es la siguiente clase MutableInt :
export class MutableInt { static { Object.setPrototypeOf(MutableInt.prototype, null); Object.freeze(MutableInt.prototype); Object.freeze(MutableInt); } #value : number constructor (value : number = 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; } } ¿ MutableInt está correctamente bloqueado? ¿O hay algo mal o que falta con este enfoque?
Editar: para aclarar, no solo estoy interesado en los decoradores finales, etc., estoy interesado en todo tipo de formas en que MutableInt podría manipularse de tal manera que no sea seguro usarlo. Parece que en TypeScript/JavaScript hay un millón de vectores de ataque en los que una API puede verse comprometida, y me pregunto si el código anterior los considera todos o se pierde algo.