Declaration Merging | Typescript
Introduction#
declaration merging means that the compiler merges two separate declarations declared with the same name into a single definition.
This merged definition has the features of both of the original declarations.
Any number of declarations can be merged; it's not limited to just two declarations.
Basic Concepts#
In TypeScript, a declaration creates entities in at least one of three groups: namespace, type, or value.
| Declaration Type | Namespace | Type | Value |
|---|---|---|---|
| Namespace | X | X | |
| Class | X | X | |
| Enum | X | X | |
| Interface | X | ||
| Type Alias | X | ||
| Function | X | ||
| Variable | X |
Merging Interfaces#
At the most basic level, the merge mechanically joins the members of both declarations into a single interface with the same name.
Non-function members of the interfaces should be unique. If they are not unique, they must be of the same type. The compiler will issue an error if the interfaces both declare a non-function member of the same name, but of different types.
For function members, each function member of the same name is treated as describing an overload of the same function.
The three interfaces will merge to create a single declaration as so:
If a signature has a parameter whose type is a single string literal type (e.g. not a union of string literals), then it will be bubbled toward the top of its merged overload list.
For instance, the following interfaces will merge together:
The resulting merged declaration of Document will be the following:
Merging Namespaces#
is equivalent to:
Non-exported members are only visible in the original (un-merged) namespace. This means that after merging, merged members that came from other declarations cannot see non-exported members.
Merging Namespaces with Classes, Functions, and Enums#
Merging Namespaces with Classes#
This gives the user a way of describing inner classes.
We must export the AlbumLabel class for the merged class to see it.
The end result is a class managed inside of another class.
You can also use namespaces to add more static members to an existing class.
You may also be familiar with the JavaScript practice of creating a function and then extending the function further by adding properties onto the function.
Similarly, namespaces can be used to extend enums with static members:
Disallowed Merges#
Not all merges are allowed in TypeScript. Currently, classes can not merge with other classes or with variables. For information on mimicking class merging, see the Mixins in TypeScript section.
Module Augmentation#
Although JavaScript modules do not support merging, you can patch existing objects by importing and then updating them.
This works fine in TypeScript too, but the compiler doesn't know about Observable.prototype.map.
You can use module augmentation to tell the compiler about it:
The module name is resolved the same way as module specifiers in import/export.
See Modules for more information.
Then the declarations in an augmentation are merged as if they were declared in the same file as the original.
However, there are two limitations to keep in mind:
- You can't declare new top-level declarations in the augmentation -- just patches to existing declarations.
- Default exports also cannot be augmented, only named exports (since you need to augment an export by its exported name, and
defaultis a reserved word - see #14080 for details)
Global augmentation#
You can also add declarations to the global scope from inside a module:
Global augmentations have the same behavior and limits as module augmentations.