Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
¿Bloquear una clase de TypeScript para que sea definitiva y todas las invariantes estén garantizadas, incluso en tiempo de ejecución?

¿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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!