Merging Function Overloads in Interfaces
What you'll learn: When two interfaces with the same name both declare a method, TypeScript combines their function signatures into overloads on a single method.
The Core Idea
You already know that interfaces can be merged through declaration merging—when you declare two interfaces with the same name, TypeScript combines their properties. But what happens when both interfaces declare a method with the same name?
Instead of causing a conflict, TypeScript merges the function signatures into overloads for that method. The signatures accumulate, giving you a method with multiple call signatures—just like you learned with function overloads.
How It Works
When you declare the same method across merged interfaces, TypeScript treats each signature as an overload:
interface Logger {
log(message: string): void;
}
interface Logger {
log(level: number, message: string): void;
}
// The merged interface has both overloads
const myLogger: Logger = {
log(msgOrLevel: string | number, message?: string): void {
if (typeof msgOrLevel === 'number') {
console.log(`[${msgOrLevel}] ${message}`);
} else {
console.log(msgOrLevel);
}
}
};
myLogger.log("Hello"); // First overload
myLogger.log(3, "Warning"); // Second overload
The implementation signature must be compatible with both overloads, just like regular function overloads.
Order Matters
Later declarations appear first in overload resolution order—TypeScript checks them from most-recently-declared to earliest.
Key Takeaway: When interfaces with the same name declare the same method, TypeScript merges their signatures into function overloads, allowing a single method to accept multiple parameter patterns.