Created
April 14, 2020 04:27
-
-
Save chiro-hiro/04f288f29aefb2c1d32917fdc15057d3 to your computer and use it in GitHub Desktop.
Singleton in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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