Sub topic 10
Declaration merging and when it helps
This chapter assumes you know basic TypeScript annotations, JavaScript objects, and classes. You should also be able to run the TypeScript compiler or read type errors in an editor.
Declaration merging is a TypeScript feature that combines multiple declarations with the same name into one type. It mainly applies to interfaces. This can help when several parts of an application contribute to one public shape, but it can also make code harder to find and understand.
Merging interface declarations
When TypeScript sees two interfaces with the same name, it merges their members:
interface Config {
host: string;
}
interface Config {
port: number;
secure?: boolean;
}
const config: Config = {
host: "localhost",
port: 8080,
};
console.log(`${config.host}:${config.port}`);After merging, Config has host, port, and optional secure properties. The order of the declarations does not change the resulting shape.
Members with the same name must be compatible. In practice, a repeated property must have the same type:
interface User {
id: number;
}
// Error: subsequent property declarations must have the same type.
interface User {
// id: string;
}Methods can also be added by another declaration. This makes merging useful when a library exposes a base interface and a separate file adds an application-specific capability.
Merging versus repeating a type alias
A type alias gives a name to any type expression, including unions, tuples, and primitives. Type aliases cannot be declared twice with the same name:
type Settings = {
host: string;
};
// Error: Duplicate identifier "Settings".
/*
type Settings = {
port: number;
};
*/Use an intersection when you own both pieces and want to combine them explicitly:
type HostSettings = { host: string };
type PortSettings = { port: number };
type Settings = HostSettings & PortSettings;
const settings: Settings = {
host: "localhost",
port: 8080,
};The intersection documents the combination at the point where it happens. Declaration merging hides that combination across declarations.
| Need | Common choice |
|---|---|
| Add members to a named object contract from separate files | Interface declaration merging |
| Combine types explicitly in one location | Intersection type |
| Name a union, tuple, or literal type | Type alias |
When merging helps
Declaration merging is useful in these situations:
- A library defines an interface that applications intentionally extend.
- A plugin system lets each plugin add fields to a shared configuration type.
- A project uses module augmentation to add known members to a third-party library.
- Generated declarations and hand-written declarations must describe one public API.
A common example is extending a library's request type with authenticated user data. The application can add a user property through the library's documented augmentation mechanism instead of using unchecked casts throughout the codebase.
Surprises and team rules
Merging can be difficult to locate. An editor may show the final interface, while the members are spread across several files. Accidental reuse of a common interface name can silently add properties to a global type.
A practical team convention is:
- Use interfaces for public object contracts that consumers may extend.
- Use type aliases for unions, intersections, tuples, and local composition.
- Keep merged declarations close together or document the augmentation.
- Avoid relying on merging merely to avoid writing a new type name.
Exercise
What happens when two interface declarations have the same name, and how is that different from repeating a type alias?
Show answer
TypeScript merges same-named interface declarations into one contract, but repeating a type alias name produces a duplicate-identifier error.