TypeScript namespace merging explained

TypeScript namespace merging explained

Preface:
Reviewing the content of the previous section, in the previous section we introduced the most common declaration merging in TS: interface merging

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 name

Similar 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:

  • The interfaces with the same name exported by the module will be merged into one interface
  • Non-exported members are visible only within their original (pre-merge) namespace. That is to say, after the merge, members merged from other namespaces cannot access non-exported members.
  • For the merging of values, if the names of the values ​​are the same, the value of the later namespace will have a higher priority.
  • For members without conflicts, they will be directly mixed in

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, Animals , are eventually merged into one namespace, and the result is that three non-conflicting things are directly mixed together.

Namespaces and other types of merging

Namespaces can be combined with other types of declarations, such as classes and functions, and enumeration types.

Merge namespaces and classes with the same name

For 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 JavaScript to create a function and later extend it to add some properties. TypeScript uses declaration merging to achieve this and ensure type safety.

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 name

It 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;
        }
    }
}

Notice:
Not everything can be merged. Note that classes cannot be merged with other classes or variables.

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:
  • TypeScript namespace explanation
  • A brief analysis of TypeScript namespace

<<:  Detailed explanation of MySQL high availability architecture

>>:  Detailed explanation of the usage of image tags in HTML

Recommend

Share a Markdown editor based on Ace

I think editors are divided into two categories, ...

In-depth analysis of the Linux kernel macro container_of

1. As mentioned above I saw this macro when I was...

Regarding the Chinese garbled characters in a href parameter transfer

When href is needed to pass parameters, and the p...

W3C Tutorial (11): W3C DOM Activities

The Document Object Model (DOM) is a platform, a ...

How to use Navicat to export and import mysql database

MySql is a data source we use frequently. It is v...

How to handle the tcp_mark_head_lost error reported by the Linux system

Problem Description Recently, a host reported the...

Docker installation and configuration image acceleration implementation

Table of contents Docker version Install Docker E...

A thorough analysis of HTML special characters

A Thorough Analysis of HTML (14) Special Characte...

Solution to prevent caching in pages

Solution: Add the following code in <head>: ...

Implementation of navigation bar and drop-down menu in CSS

1. CSS Navigation Bar (1) Function of the navigat...

jQuery achieves full screen scrolling effect

This article example shares the specific code of ...

Docker installation steps for Redmine

Download the image (optional step, if omitted, it...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...

Implementation of check constraints in MySQL 8.0

Hello everyone, I am Tony, a teacher who only tal...