Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.