Is this a bad practice in Typescript? What are my options?

668 Views Asked by At

I want to have a consistent instance of a class (a collection in this example) to work in the whole project with. I am using Typescript so I want to work around the concept of creating some wonky object in the JavaScript-style without any type safety.

As I don't need to pass a real object of this class anywhere, I am completely fine with the idea of having a class with only static members, which I can access in the project by calling the class name. Is there anything wrong with this concept? Should I use a singleton pattern and then just do export Collection.getInstance() at the end of the file?

export class CollectionInstance{
  static readonly exampleCollection = new ExampleCollection();

  //noinspection JSUnusedLocalSymbols
  private constructor() {
  }

  public static load(){

  }

  public static get(key: string){

  }
}
0

There are 0 best solutions below