typescript 38 lines · 7 steps

A module-scoped Singleton in TypeScript

Module caching guarantees one shared ConfigManager instance, with typed get/set accessors over a private config object.

Explained by highlit
1// A module-scoped Singleton: the single instance lives in this module's
2// closure and is never exposed directly.
3 
4interface AppConfig {
5 apiUrl: string;
6 retries: number;
7}
8 
9class ConfigManager {
10 private config: AppConfig;
11 
12 constructor() {
13 this.config = {
14 apiUrl: process.env.API_URL ?? "http://localhost:3000",
15 retries: Number(process.env.RETRIES ?? 3),
16 };
17 }
18 
19 get<K extends keyof AppConfig>(key: K): AppConfig[K] {
20 return this.config[key];
21 }
22 
23 set<K extends keyof AppConfig>(key: K, value: AppConfig[K]): void {
24 this.config[key] = value;
25 }
26 
27 snapshot(): Readonly<AppConfig> {
28 return Object.freeze({ ...this.config });
29 }
30}
31 
32// Instantiated once at module load; module caching guarantees a single
33// instance shared across every importer.
34const instance = new ConfigManager();
35 
36export function getConfigManager(): ConfigManager {
37 return instance;
38}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1ES modules are cached, so a const initialized at load time becomes a natural singleton without any locking.
  2. 2Keying generics on `keyof` ties an accessor's return type to the property you ask for, catching typos at compile time.
  3. 3Returning a frozen shallow copy lets callers read state safely without mutating the source of truth.

Related explainers

Share this explainer

Here's the card — post it anywhere.

A module-scoped Singleton in TypeScript — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code