I have a class MyStore that contains data and state for my JS application. I want to get one instance when importing this class into other files (I use commonjs aka external modules).
Example:
Mystore.ts
class MyStore extends BaseStore { private _items; getItems() { return _items; } } export = MyStore;
OtherFile.ts:
import MyStore = require('./MyStore'); MyStore.getItems();
this does not work as I have to impose MyStore in the Otherfile.ts file
My current solution is to have a static singleton method:
MyStore.ts:
private static _instance = new MyStore(); static getInstance(){ return this._instance;}
OtherFile.ts
import MyStore = require('./MyStore'); MyStore.getInstance().getItems();
Is there a better way?
Edit: the reason this is a Typescript class is to allow inheritance (extension of the base class)
source share