Skip to content

Instantly share code, notes, and snippets.

@chiro-hiro
Created April 14, 2020 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiro-hiro/04f288f29aefb2c1d32917fdc15057d3 to your computer and use it in GitHub Desktop.
Save chiro-hiro/04f288f29aefb2c1d32917fdc15057d3 to your computer and use it in GitHub Desktop.
Singleton in TypeScript
const instanceMap: {[key:string]: any} = {};
export function Singleton<T>(instanceName:string, InstanceConstructor: new () => T):T {
if (typeof instanceMap[instanceName] === 'undefined') {
instanceMap[instanceName] = new InstanceConstructor();
}
return <T>instanceMap[instanceName];
}
export default Singleton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment