“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

Namespaces are named in the following way:

In keeping with ECMAScript 2015 terminology, starting with TypeScript 1.5, “external modules” are called “modules” and “internal modules” are called “namespaces.”

To avoid confusing new users with similar names, it is recommended that:

Any place where the module keyword is used to declare an internal module should be replaced with the namespace keyword

Specific uses will be covered below

Using namespaces

The way to use a namespace is actually very simple, the format is as follows:

namespace X {}
Copy the code

See the following example for specific usage (example source: TS official documentation)

Let’s define a few simple string validators, assuming we’ll use them to validate user input in a form or to validate external data

interface StringValidator {
    isAcceptable(s: string) :boolean;
}

let lettersRegexp = /^[A-Za-z]+$/;
let numberRegexp = / ^ [0-9] + $/;

class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
        returnlettersRegexp.test(s); }}class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
        return s.length === 5&& numberRegexp.test(s); }}// Some samples to try
let strings = ["Hello"."98052"."101"];

// Validators to use
let validators: { [s: string]: StringValidator; } = {};
validators["ZIP code"] = new ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidator();

// Show whether each string passed each validator
for (let s of strings) {
    for (let name in validators) {
        let isMatch = validators[name].isAcceptable(s);
        console.log(` '${ s }' ${ isMatch ? "matches" : "does not match" } '${ name }'. `); }}Copy the code

Now we put all the validators in one file

However, as more validators are added, we may worry about naming conflicts with other objects. So we use namespaces to organize our code

Use the following namespace:

namespace Validation {
    export interface StringValidator {
        isAcceptable(s: string) :boolean;
    }

    const lettersRegexp = /^[A-Za-z]+$/;
    const numberRegexp = / ^ [0-9] + $/;

    export class LettersOnlyValidator implements StringValidator {
        isAcceptable(s: string) {
            returnlettersRegexp.test(s); }}export class ZipCodeValidator implements StringValidator {
        isAcceptable(s: string) {
            return s.length === 5&& numberRegexp.test(s); }}}// Some samples to try
let strings = ["Hello"."98052"."101"];

// Validators to use
let validators: { [s: string]: Validation.StringValidator; } = {};
validators["ZIP code"] = new Validation.ZipCodeValidator();
validators["Letters only"] = new Validation.LettersOnlyValidator();

// Show whether each string passed each validator
for (let s of strings) {
    for (let name in validators) {
        console.log(`"${ s }"-${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`); }}Copy the code

In the code above, put all the types associated with the validator into a namespace called Validation. Because we want these interfaces and classes to be accessible outside of the namespace, we need to use export. In contrast, the variables lettersRegexp and numberRegexp are implementation details that do not need to be exported, so they are not accessible outside of the namespace

One problem is that if it’s just one file, it becomes difficult to maintain as the application gets bigger and bigger, so we optionally split the single file into different files as needed

We’ll continue with this in the next section, with multi-file namespaces, and we’ll split the single file in the previous example into multiple files. Welcome to attention

END

That’s all for this article. If you have any questions, please point out