Incrementally extend shared interfaces
Add properties to an interface across separate files or feature modules without editing its original declaration.
TypeScript · Function
Declaration merging combines compatible same-named TypeScript declarations into one definition. It supports patterns such as interface extension by repeated declarations, namespace combination, and module augmentation.
TypeScript combines compatible declarations that share a name into one definition. The result depends on the declaration kinds: interfaces join members, namespaces can add exported members, and module augmentations patch existing declarations; unsupported or conflicting combinations produce errors.
interface Name { ... }
interface Name { ... }
namespace Name { ... }
// Module augmentation
declare module "module-name" { ... }interface User {
id: string;
}
interface User {
displayName: string;
}
const user: User = { id: "u1", displayName: "Ada" };
console.log(user.displayName);The two declarations combine into one User interface, so objects must provide members from both declarations.
Add properties to an interface across separate files or feature modules without editing its original declaration.
Attach static-like namespaces or exported helpers to an existing function, class, or enum.
Add type information for a library plugin or missing third-party module member.
Describe callable and constructable declaration patterns in declaration files where supported.
Declaration merging combines separate declarations with the same name into one definition, and the merged result exposes features from each declaration. Multiple declarations can participate, not only two.
Follow-up: Which interface members may conflict? Non-function members must be unique, while repeated function members are treated as overloads.
Interface merging mechanically joins the members of declarations with the same interface name. A repeated non-function member is allowed only when its type agrees across declarations.
Follow-up: What happens to repeated methods? They contribute overload signatures, with later declarations generally ordered before earlier ones in the overload set.
Module augmentation lets a declaration patch an existing module, and its declarations merge as though written beside the original. The augmentation cannot add a new top-level declaration.
Follow-up: How is the target module found? Its name follows the same resolution rules as module specifiers used by import and export.
interface mergingnamespace mergingmodule augmentationglobal augmentationMixins in TypeScript