sussy-util
Version:
Util package made by me
30 lines (29 loc) • 1.44 kB
TypeScript
import { Constructor, AnyFunction } from '../Types';
export default class ImmutableClass<T extends object> {
/**
* Creates an instance of the ImmutableClass.
* Freezing the prototype of the extending class or any class instance created thereafter is crucial to prevent potential safety issues resulting from prototype modifications.
*
* @param {Constructor<T>} constructor - The constructor function of the class.
* @param {Array<[string, () => unknown]>} properties - The array of property definitions.
* Each inner array should contain a property name and its corresponding getter function.
* @param {Array<[string, AnyFunction]>} functions - The array of function definitions.
* Each inner array should contain a function name and its corresponding function.
* @throws {Error} Throws an error if the class is extended by another class.
*
* @example Example usage:
* class TestClass extends ImmutableClass {
* #a = 12;
*
* constructor() {
* super(TestClass, [["a", () => this.#a]], [["displayA", () => console.log(this.#a)]]);
* }
*}
* Objects.freeze(TestClass.prototype);
*
* const test = new TestClass();
* test.a // 12
* test.a = 13 // throws error
*/
constructor(constructor: Constructor<T>, properties: [string, () => unknown][], functions: [string, AnyFunction][]);
}