Skip to content
TypeScript

Declaration Merging

Merge multiple declarations with the same name.

By EZ4Code Team
mergedeclaration

Code

interface Box {
  width: number;
}

interface Box {
  height: number;
}

// After merging
const box: Box = { width: 10, height: 20 };

// Function merging
function fn(x: number): number;
function fn(x: string): string;
function fn(x: any): any { return x; }

// Namespace merging into class
class Counter {}
namespace Counter {
  export const instances = 0;
}

Explanation

Interfaces with the same name automatically merge properties, useful for extending third-party types.

More TypeScript Snippets