What is the best and most convenient way to implement a Singleton pattern for a class in TypeScript? (Both with and without lazy initialisation).
i think maybe use generics be batter
class Singleton{
public static Instance(c: {new(): T; }) : T{
if (this._instance == null){
this._instance = new c();
}
return this._instance;
}
private static _instance = null;
}
how to use
step1
class MapManager extends Singleton{
//do something
public init():void{ //do }
}
step2
MapManager.Instance(MapManager).init();