Skip to content

Instantly share code, notes, and snippets.

@DimitryDushkin
Created November 16, 2018 09:25
Show Gist options
  • Save DimitryDushkin/783776232e84ef5425fcb4de2b923de7 to your computer and use it in GitHub Desktop.
Save DimitryDushkin/783776232e84ef5425fcb4de2b923de7 to your computer and use it in GitHub Desktop.
TypeScript singleton (swift style)
export default class SomeSingleton {
private static privateShared: Api;
public static get shared(): Api {
if (!SomeSingleton.privateShared) {
SomeSingleton.privateShared = new SomeSingleton();
}
return SomeSingleton.privateShared;
}
public foo() {}
}
// Using
import SomeSingleton from './some-singleton';
SomeSingleton.shared.foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment