Careeroza
Declaration Merging
Interfaces with the Same Name Merge
If you declare the same interface twice in the same scope, TypeScript merges them into one. Libraries use this so that you can add fields to their types (for example adding user to Express Request, covered in the advanced level).
Example
interface AppConfig {
apiUrl: string;
}
interface AppConfig {
timeoutMs: number;
}
const config: AppConfig = { apiUrl: "https://api.shopease.in", timeoutMs: 5000 };
console.log(config);Output
{ apiUrl: 'https://api.shopease.in', timeoutMs: 5000 }Type Aliases Cannot Merge
Example
Declaring a type twice is an error (TS2300 Duplicate identifier).
type AppConfig = { apiUrl: string };
type AppConfig = { timeoutMs: number };Compiler output (tsc)
Line 1: error TS2300: Duplicate identifier 'AppConfig'. Line 2: error TS2300: Duplicate identifier 'AppConfig'.
Note: Merging can also surprise you: if two files accidentally use the same global interface name, their fields get mixed. Keep your types inside modules (files with import/export).