Skip to content

Instantly share code, notes, and snippets.

@ariesmcrae
Created June 9, 2022 11:43
Show Gist options
  • Save ariesmcrae/1285d2cb354d2a05735f10315ea79fe4 to your computer and use it in GitHub Desktop.
Save ariesmcrae/1285d2cb354d2a05735f10315ea79fe4 to your computer and use it in GitHub Desktop.
node.js singleton example in typescript
class Logger {
public error(msg: string, exception: Error): void {
console.error(msg, exception);
}
}
const instance = new Logger();
// Prevent modification to properties and values of the object.
Object.freeze(instance);
// node.js module based singleton
export { instance as MyLogger };
// Usage:
//
// import { MyLogger } from './Logger';
// MyLogger.error('Error message', new Error('My Error message'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment