🔷 TypeScript Intermediate

What is module augmentation in TypeScript?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

Module augmentation lets you add new declarations to an existing module or library without modifying its source code. This is how you extend third-party library types. To augment a module, import it and re-declare it in a declare module block: declare module "express" { interface Request { user?: User; currentTenant?: Tenant; } } — now Express's Request type includes your custom properties. This is typically placed in a .d.ts file (e.g., types/express.d.ts) and included in tsconfig. Global augmentation: extend the global scope by wrapping in declare global { ... } inside a module file. Module augmentation uses declaration merging internally — TypeScript merges the augmented declarations with the original module's declarations. Common use cases: adding custom properties to Express Request, extending Window, augmenting Vuex store types, adding methods to built-in prototypes. Module augmentation works only with ES module syntax — ambient modules use a different pattern.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.