Preface: We understand that declaration merging actually means that the compiler will merge declarations with the same name into one declaration. The result of the merging is that the merged declaration will have the characteristics of the original two or more declarations at the same time. The interface merge requires whether the members inside have function members. For the function members inside, each function declaration with the same name will be treated as an overload of this function. When interface A is merged with the later interface A, the latter interface has a higher priority. Today we are also going to talk about declaration merging in TS, but this time it is about namespace-related merging There are two main aspects: one is the merging of namespaces with the same name, and the other is the merging of namespaces with other types. The following will describe one by one Merge namespaces with the same nameSimilar to interface merging, two or more namespaces with the same name will also have their members merged. So how do we merge it specifically? When merging namespaces with the same name, remember the following four points:
For example: namespace Animals { export class Cat { } } namespace Animals { export interface Legged { numberOfLegs: number; } export class Dog { } } is equivalent to: namespace Animals { export interface Legged { numberOfLegs: number; } export class Cat { } export class Dog { } } In the above example, two namespaces with the same name, Namespaces and other types of mergingNamespaces can be combined with other types of declarations, such as classes and functions, and enumeration types. Merge namespaces and classes with the same nameFor example: class Album { label: Album.AlbumLabel; } namespace Album { export class AlbumLabel { } // Export the `AlbumLabel` class so that the merged class can access it } The merger of a namespace and a class results in a class with an inner class Merge namespaces and functions with the same name In addition to the inner class pattern described above, it is also common in For example, an official example: function buildLabel(name: string): string { return buildLabel.prefix + name + buildLabel.suffix; } namespace buildLabel { export let suffix = ""; export let prefix = "Hello, "; } console.log(buildLabel("Sam Smith")); Namespace and enumeration with the same nameIt can be used to expand enumerations. Let’s look at the official examples. enum Color { red = 1, green = 2, blue = 4 } namespace Color { export function mixColor(colorName: string) { if (colorName == "yellow") { return Color.red + Color.green; } else if (colorName == "white") { return Color.red + Color.green + Color.blue; } else if (colorName == "magenta") { return Color.red + Color.blue; } else if (colorName == "cyan") { return Color.green + Color.blue; } } }
This is the end of this article about TS namespace merging. For more information about TS namespace merging, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of MySQL high availability architecture
>>: Detailed explanation of the usage of image tags in HTML
I think editors are divided into two categories, ...
1. As mentioned above I saw this macro when I was...
When href is needed to pass parameters, and the p...
This article uses examples to describe how to use...
The Document Object Model (DOM) is a platform, a ...
MySql is a data source we use frequently. It is v...
Problem Description Recently, a host reported the...
Table of contents Docker version Install Docker E...
A Thorough Analysis of HTML (14) Special Characte...
Solution: Add the following code in <head>: ...
1. CSS Navigation Bar (1) Function of the navigat...
This article example shares the specific code of ...
Download the image (optional step, if omitted, it...
Preface As you know, Linux supports many file sys...
Hello everyone, I am Tony, a teacher who only tal...