Intermediate · 2 of 16 · 13 min read
Declaration merging
Two declarations of one name combining into a single type — the rule, the limits, and the one thing it is genuinely for.
- Interfaces
Two declarations with the same name in the same scope normally collide. Interfaces do not: they combine. It looks like a quirk and it is the mechanism behind every “add a property to a library’s type” you will ever write.
The rule
interface Animal { name: string }
interface Animal { age: number }
const rex: Animal = { name: "Rex", age: 3 }; // both members required
console.log(rex.name, rex.age);
Every declaration contributes its members. Non-function members must be identical across declarations:
interface Config { port: number }
// interface Config { port: string }
// Subsequent property declarations must have the same type
Methods are the exception — repeating one adds an overload rather than conflicting:
interface Api {
get(path: string): string;
}
interface Api {
get(path: string, retries: number): string;
}
declare const api: Api;
console.log(typeof api.get); // one method, two signatures
Later declarations come first in the overload list, which is how the DOM types manage to put
more specific signatures ahead of general ones across several lib files.
| Declared twice | Result |
|---|---|
interface |
merges |
type alias |
Duplicate identifier |
namespace |
merges |
enum |
merges, if one is const-free and values do not clash |
class + interface |
merges — the interface adds members to the instance type |
function + namespace |
merges — properties on the function |
What it is for
Three real uses, in rough order of how often you will meet them.
Adding to a library’s types
// Express attaches a `user` in middleware; its own types know nothing about it.
import type { Request } from "express";
declare module "express" {
interface Request {
user?: { id: number; role: string };
}
}
function handler(req: Request) {
console.log(req.user?.role); // typed
}
console.log(typeof handler);
The import is what makes this an augmentation rather than a redeclaration of the whole module.
Without it, declare module "express" would replace Express’s types entirely.
Adding to a global
declare global {
interface Window {
analytics?: { track(event: string): void };
}
}
export {}; // makes this file a module, which `declare global` requires
// elsewhere:
// window.analytics?.track("signup");
Window, globalThis, Array, String — all interfaces, all augmentable. Which also means a
careless augmentation is visible everywhere, so it belongs in a clearly named .d.ts rather than
halfway down a feature file.
Typing environment variables
declare global {
namespace NodeJS {
interface ProcessEnv {
DATABASE_URL: string;
NODE_ENV: "development" | "production" | "test";
}
}
}
export {};
// process.env.DATABASE_URL is now string, not string | undefined
Merging across kinds
A class declaration creates both a value and a type, and an interface of the same name adds to the type side:
class Point {
constructor(public x: number, public y: number) {}
}
interface Point {
describe(): string; // declared here, implemented below
}
Point.prototype.describe = function () {
return `(${this.x}, ${this.y})`;
};
console.log(new Point(1, 2).describe()); // "(1, 2)"
That is how a library adds methods to a prototype and keeps the types honest. A function and a namespace merge for the same reason — the namespace adds properties to the function value:
function greet(name: string) { return `Hi ${name}`; }
namespace greet {
export const version = "1.0";
}
console.log(greet("Ana"), greet.version); // "Hi Ana" "1.0"
Why an application usually does not want it
In your own code, two interfaces with one name is a mistake that produces no error:
// user.ts — no import or export, so this file is a *script*, not a module
interface User { id: number }
// admin.ts — also a script
interface User { name: string }
// Both are global, so they merge: User is now { id: number; name: string }
// and nothing told you.
The fix is the thing that fixes most of these: make the files modules.
// user.ts
export interface User { id: number }
// admin.ts
export interface User { name: string } // a different, unrelated User
A file with no top-level import or export contributes to the global scope. One export {} is
enough to stop it.
Merging is not inheritance
interface Base { id: number }
interface Base { name: string } // merged into Base itself
interface Derived extends Base { role: string } // a new, separate type
const b: Base = { id: 1, name: "Ana" };
const d: Derived = { id: 1, name: "Ana", role: "admin" };
console.log(b.name, d.role);
Merging changes the original type everywhere it is used. Extending creates a new one. If what you
want is “the same but with more”, you want extends.
Common mistakes
Forgetting the import in a module augmentation
Mistake. declare module "express" { interface Request { user?: User } } with no import in
the file.
Why it fails. Without an import or export the file is a script, so declare module declares a
new ambient module rather than augmenting the real one — and the real one’s types vanish.
What to do instead. import type { Request } from "express" at the top, or export {} at the
bottom.
Augmenting a global from a feature file
Mistake. declare global { interface Window { … } } inside a component.
Why it fails. It works, and it is invisible: the next person has no idea where window.foo
came from.
What to do instead. One types/global.d.ts, named so it is obvious.
Relying on merging inside your own code
Mistake. Splitting one interface across two files “to keep them small”.
Why it fails. Nothing shows the full shape, errors mention members from a file you are not
looking at, and a typo in the name silently creates a second type instead of failing.
What to do instead. One declaration, and extends when you want a variant.
Check yourself
One question from the bank on what you just read. Answering here does not affect your quiz history.
Two interfaces with the same name in the same scope do not collide — they combine.
What shape does Animal have after both declarations?
interface Animal {
name: string;
}
interface Animal {
age: number;
}Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.