{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/declaration-merging.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"2c54aa33-56cf-50c4-bb95-2986d2e0e1ac","excerpt":"Introduction Some of the unique concepts in TypeScript describe the shape of JavaScript objects at the type level.\nOne example that is especially unique to…","html":"<h1 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h1>\n<p>Some of the unique concepts in TypeScript describe the shape of JavaScript objects at the type level.\nOne example that is especially unique to TypeScript is the concept of ‘declaration merging’.\nUnderstanding this concept will give you an advantage when working with existing JavaScript.\nIt also opens the door to more advanced abstraction concepts.</p>\n<p>For the purposes of this article, “declaration merging” means that the compiler merges two separate declarations declared with the same name into a single definition.\nThis merged definition has the features of both of the original declarations.\nAny number of declarations can be merged; it’s not limited to just two declarations.</p>\n<h1 id=\"basic-concepts\" style=\"position:relative;\"><a href=\"#basic-concepts\" aria-label=\"basic concepts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Basic Concepts</h1>\n<p>In TypeScript, a declaration creates entities in at least one of three groups: namespace, type, or value.\nNamespace-creating declarations create a namespace, which contains names that are accessed using a dotted notation.\nType-creating declarations do just that: they create a type that is visible with the declared shape and bound to the given name.\nLastly, value-creating declarations create values that are visible in the output JavaScript.</p>\n<table>\n<thead>\n<tr>\n<th>Declaration Type</th>\n<th align=\"center\">Namespace</th>\n<th align=\"center\">Type</th>\n<th align=\"center\">Value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Namespace</td>\n<td align=\"center\">X</td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n</tr>\n<tr>\n<td>Class</td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n<td align=\"center\">X</td>\n</tr>\n<tr>\n<td>Enum</td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n<td align=\"center\">X</td>\n</tr>\n<tr>\n<td>Interface</td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n<td align=\"center\"></td>\n</tr>\n<tr>\n<td>Type Alias</td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n<td align=\"center\"></td>\n</tr>\n<tr>\n<td>Function</td>\n<td align=\"center\"></td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n</tr>\n<tr>\n<td>Variable</td>\n<td align=\"center\"></td>\n<td align=\"center\"></td>\n<td align=\"center\">X</td>\n</tr>\n</tbody>\n</table>\n<p>Understanding what is created with each declaration will help you understand what is merged when you perform a declaration merge.</p>\n<h1 id=\"merging-interfaces\" style=\"position:relative;\"><a href=\"#merging-interfaces\" aria-label=\"merging interfaces permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Merging Interfaces</h1>\n<p>The simplest, and perhaps most common, type of declaration merging is interface merging.\nAt the most basic level, the merge mechanically joins the members of both declarations into a single interface with the same name.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Box {</span>\n<span style=\"color: #000000\">  height: number;</span>\n<span style=\"color: #000000\">  width: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Box {</span>\n<span style=\"color: #000000\">  scale: number;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> box: Box = { height: </span><span style=\"color: #09835A\">5</span><span style=\"color: #000000\">, width: </span><span style=\"color: #09835A\">6</span><span style=\"color: #000000\">, scale: </span><span style=\"color: #09835A\">10</span><span style=\"color: #000000\"> };</span></code></div></pre>\n<p>Non-function members of the interfaces should be unique.\nIf they are not unique, they must be of the same type.\nThe compiler will issue an error if the interfaces both declare a non-function member of the same name, but of different types.</p>\n<p>For function members, each function member of the same name is treated as describing an overload of the same function.\nOf note, too, is that in the case of interface <code>A</code> merging with later interface <code>A</code>, the second interface will have a higher precedence than the first.</p>\n<p>That is, in the example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Cloner {</span>\n<span style=\"color: #000000\">  clone(animal: Animal): Animal;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Cloner {</span>\n<span style=\"color: #000000\">  clone(animal: Sheep): Sheep;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Cloner {</span>\n<span style=\"color: #000000\">  clone(animal: Dog): Dog;</span>\n<span style=\"color: #000000\">  clone(animal: Cat): Cat;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The three interfaces will merge to create a single declaration as so:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Cloner {</span>\n<span style=\"color: #000000\">  clone(animal: Dog): Dog;</span>\n<span style=\"color: #000000\">  clone(animal: Cat): Cat;</span>\n<span style=\"color: #000000\">  clone(animal: Sheep): Sheep;</span>\n<span style=\"color: #000000\">  clone(animal: Animal): Animal;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Notice that the elements of each group maintains the same order, but the groups themselves are merged with later overload sets ordered first.</p>\n<p>One exception to this rule is specialized signatures.\nIf a signature has a parameter whose type is a <em>single</em> string literal type (e.g. not a union of string literals), then it will be bubbled toward the top of its merged overload list.</p>\n<p>For instance, the following interfaces will merge together:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Document {</span>\n<span style=\"color: #000000\">  createElement(tagName: any): Element;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Document {</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"div\"</span><span style=\"color: #000000\">): HTMLDivElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"span\"</span><span style=\"color: #000000\">): HTMLSpanElement;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Document {</span>\n<span style=\"color: #000000\">  createElement(tagName: string): HTMLElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"canvas\"</span><span style=\"color: #000000\">): HTMLCanvasElement;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The resulting merged declaration of <code>Document</code> will be the following:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Document {</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"canvas\"</span><span style=\"color: #000000\">): HTMLCanvasElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"div\"</span><span style=\"color: #000000\">): HTMLDivElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: </span><span style=\"color: #A31515\">\"span\"</span><span style=\"color: #000000\">): HTMLSpanElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: string): HTMLElement;</span>\n<span style=\"color: #000000\">  createElement(tagName: any): Element;</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"merging-namespaces\" style=\"position:relative;\"><a href=\"#merging-namespaces\" aria-label=\"merging namespaces permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Merging Namespaces</h1>\n<p>Similarly to interfaces, namespaces of the same name will also merge their members.\nSince namespaces create both a namespace and a value, we need to understand how both merge.</p>\n<p>To merge the namespaces, type definitions from exported interfaces declared in each namespace are themselves merged, forming a single namespace with merged interface definitions inside.</p>\n<p>To merge the namespace value, at each declaration site, if a namespace already exists with the given name, it is further extended by taking the existing namespace and adding the exported members of the second namespace to the first.</p>\n<p>The declaration merge of <code>Animals</code> in this example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Animals {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Zebra {}</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Animals {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Legged {</span>\n<span style=\"color: #000000\">    numberOfLegs: number;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Dog {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>is equivalent to:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Animals {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Legged {</span>\n<span style=\"color: #000000\">    numberOfLegs: number;</span>\n<span style=\"color: #000000\">  }</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Zebra {}</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Dog {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This model of namespace merging is a helpful starting place, but we also need to understand what happens with non-exported members.\nNon-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.</p>\n<p>We can see this more clearly in this example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> haveMuscles = </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> animalsHaveMuscles() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> haveMuscles;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Animal {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> doAnimalsHaveMuscles() {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> haveMuscles; </span><span style=\"color: #008000\">// Error, because haveMuscles is not accessible here</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Because <code>haveMuscles</code> is not exported, only the <code>animalsHaveMuscles</code> function that shares the same un-merged namespace can see the symbol.\nThe <code>doAnimalsHaveMuscles</code> function, even though it’s part of the merged <code>Animal</code> namespace can not see this un-exported member.</p>\n<h1 id=\"merging-namespaces-with-classes-functions-and-enums\" style=\"position:relative;\"><a href=\"#merging-namespaces-with-classes-functions-and-enums\" aria-label=\"merging namespaces with classes functions and enums permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Merging Namespaces with Classes, Functions, and Enums</h1>\n<p>Namespaces are flexible enough to also merge with other types of declarations.\nTo do so, the namespace declaration must follow the declaration it will merge with. The resulting declaration has properties of both declaration types.\nTypeScript uses this capability to model some of the patterns in JavaScript as well as other programming languages.</p>\n<h2 id=\"merging-namespaces-with-classes\" style=\"position:relative;\"><a href=\"#merging-namespaces-with-classes\" aria-label=\"merging namespaces with classes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Merging Namespaces with Classes</h2>\n<p>This gives the user a way of describing inner classes.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Album {</span>\n<span style=\"color: #000000\">  label: Album.AlbumLabel;</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Album {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> AlbumLabel {}</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The visibility rules for merged members is the same as described in the ‘Merging Namespaces’ section, so we must export the <code>AlbumLabel</code> class for the merged class to see it.\nThe end result is a class managed inside of another class.\nYou can also use namespaces to add more static members to an existing class.</p>\n<p>In addition to the pattern of inner classes, 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.\nTypeScript uses declaration merging to build up definitions like this in a type-safe way.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> buildLabel(name: string): string {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> buildLabel.prefix + name + buildLabel.suffix;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> buildLabel {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> suffix = </span><span style=\"color: #A31515\">\"\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> prefix = </span><span style=\"color: #A31515\">\"Hello, \"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">console.log(buildLabel(</span><span style=\"color: #A31515\">\"Sam Smith\"</span><span style=\"color: #000000\">));</span></code></div></pre>\n<p>Similarly, namespaces can be used to extend enums with static members:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">enum</span><span style=\"color: #000000\"> Color {</span>\n<span style=\"color: #000000\">  red = </span><span style=\"color: #09835A\">1</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">  green = </span><span style=\"color: #09835A\">2</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">  blue = </span><span style=\"color: #09835A\">4</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">namespace</span><span style=\"color: #000000\"> Color {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\"> mixColor(colorName: string) {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (colorName == </span><span style=\"color: #A31515\">\"yellow\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Color.red + Color.green;</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (colorName == </span><span style=\"color: #A31515\">\"white\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Color.red + Color.green + Color.blue;</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (colorName == </span><span style=\"color: #A31515\">\"magenta\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Color.red + Color.blue;</span>\n<span style=\"color: #000000\">    } </span><span style=\"color: #0000FF\">else</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">if</span><span style=\"color: #000000\"> (colorName == </span><span style=\"color: #A31515\">\"cyan\"</span><span style=\"color: #000000\">) {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> Color.green + Color.blue;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<h1 id=\"disallowed-merges\" style=\"position:relative;\"><a href=\"#disallowed-merges\" aria-label=\"disallowed merges permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Disallowed Merges</h1>\n<p>Not all merges are allowed in TypeScript.\nCurrently, classes can not merge with other classes or with variables.\nFor information on mimicking class merging, see the <a href=\"/8a7ba3af176b479d6e51ed2dc6f5ac8b/Mixins.md\">Mixins in TypeScript</a> section.</p>\n<h1 id=\"module-augmentation\" style=\"position:relative;\"><a href=\"#module-augmentation\" aria-label=\"module augmentation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Module Augmentation</h1>\n<p>Although JavaScript modules do not support merging, you can patch existing objects by importing and then updating them.\nLet’s look at a toy Observable example:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// observable.ts</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Observable&lt;T&gt; {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ... implementation left as an exercise for the reader ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// map.ts</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Observable } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./observable\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">Observable.prototype.map = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(f) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ... another exercise for the reader</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>This works fine in TypeScript too, but the compiler doesn’t know about <code>Observable.prototype.map</code>.\nYou can use module augmentation to tell the compiler about it:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// observable.ts</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Observable&lt;T&gt; {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ... implementation left as an exercise for the reader ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #008000\">// map.ts</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Observable } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./observable\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">module</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./observable\"</span><span style=\"color: #000000\"> {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Observable&lt;T&gt; {</span>\n<span style=\"color: #000000\">    map&lt;U&gt;(f: (x: T) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> U): Observable&lt;U&gt;;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n<span style=\"color: #000000\">Observable.prototype.map = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">(f) {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ... another exercise for the reader</span>\n<span style=\"color: #000000\">};</span>\n\n<span style=\"color: #008000\">// consumer.ts</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Observable } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./observable\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./map\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">let</span><span style=\"color: #000000\"> o: Observable&lt;number&gt;;</span>\n<span style=\"color: #000000\">o.map(x </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> x.toFixed());</span></code></div></pre>\n<p>The module name is resolved the same way as module specifiers in <code>import</code>/<code>export</code>.\nSee <a href=\"/b65a5c87767a578f1cf8ffe424260555/Modules.md\">Modules</a> for more information.\nThen the declarations in an augmentation are merged as if they were declared in the same file as the original.</p>\n<p>However, there are two limitations to keep in mind:</p>\n<ol>\n<li>You can’t declare new top-level declarations in the augmentation — just patches to existing declarations.</li>\n<li>Default exports also cannot be augmented, only named exports (since you need to augment an export by its exported name, and <code>default</code> is a reserved word - see <a href=\"https://github.com/Microsoft/TypeScript/issues/14080\">#14080</a> for details)</li>\n</ol>\n<h2 id=\"global-augmentation\" style=\"position:relative;\"><a href=\"#global-augmentation\" aria-label=\"global augmentation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Global augmentation</h2>\n<p>You can also add declarations to the global scope from inside a module:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// observable.ts</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Observable&lt;T&gt; {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ... still no implementation ...</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #0000FF\">declare</span><span style=\"color: #000000\"> global {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> Array&lt;T&gt; {</span>\n<span style=\"color: #000000\">    toObservable(): Observable&lt;T&gt;;</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span>\n\n<span style=\"color: #000000\">Array.prototype.toObservable = </span><span style=\"color: #0000FF\">function</span><span style=\"color: #000000\">() {</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #008000\">// ...</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>Global augmentations have the same behavior and limits as module augmentations.</p>","headings":[{"value":"Introduction","depth":1},{"value":"Basic Concepts","depth":1},{"value":"Merging Interfaces","depth":1},{"value":"Merging Namespaces","depth":1},{"value":"Merging Namespaces with Classes, Functions, and Enums","depth":1},{"value":"Merging Namespaces with Classes","depth":2},{"value":"Disallowed Merges","depth":1},{"value":"Module Augmentation","depth":1},{"value":"Global augmentation","depth":2}],"frontmatter":{"permalink":"/docs/handbook/declaration-merging.html","title":"Declaration Merging"}}},"pageContext":{"slug":"/docs/handbook/declaration-merging.html","isOldHandbook":true}}}