UNPKG

@eklabdev/blingts4

Version:

A comprehensive TypeScript decorators library implementing Stage 2 decorators for TS 4.9+

24 lines (23 loc) 743 B
/** * Ensures a class has only one instance */ export function Singleton() { return function (target) { // Symbol to store the instance const instanceKey = Symbol(); // Return a class that extends the original return class SingletonClass extends target { static instance; constructor(...args) { // Check if we already have an instance if (target[instanceKey]) { return target[instanceKey]; } // Create the instance by calling the parent constructor super(...args); // Store the instance target[instanceKey] = this; } }; }; }