ArticleZip > How Do I Use Namespaces With Typescript External Modules

How Do I Use Namespaces With Typescript External Modules

Imagine you're a builder, and your toolbox is filled with all sorts of tools, from hammers to measuring tapes. Each tool has its place and purpose, just like namespaces and external modules in Typescript. In this how-to guide, we'll dive into the world of Namespaces with Typescript External Modules to help you organize and structure your code effectively.

First things first, let's understand what namespaces and external modules are. Namespaces in Typescript are used to organize and encapsulate a set of variables, functions, interfaces, and classes. They help prevent naming conflicts and provide a logical structure to your code. On the other hand, external modules allow you to split your code into multiple files, making it easier to manage and reuse components across your application.

Now, let's talk about how to use namespaces with external modules in Typescript. To start, you'll need to create a namespace declaration using the `namespace` keyword followed by the desired namespace name. Inside the namespace block, you can define various entities like classes, interfaces, and functions that belong to that namespace.

Typescript

namespace MyNamespace {
    export interface Person {
        name: string;
        age: number;
    }

    export class Employee {
        constructor(private name: string, private salary: number) {
            // Constructor code here
        }

        getDetails() {
            return `Name: ${this.name}, Salary: ${this.salary}`;
        }
    }
}

In the code snippet above, we have created a namespace called `MyNamespace` that contains an interface `Person` and a class `Employee`. Notice the `export` keyword used before each entity declaration. This keyword allows entities to be accessed outside of the namespace block.

To use the entities defined in the namespace, you need to import them in your main file or module. You can achieve this by using the `import` statement followed by the desired entity and namespace name.

Typescript

import { MyNamespace } from './myModule';

const john: MyNamespace.Person = {
    name: 'John Doe',
    age: 30
};

const employee = new MyNamespace.Employee('Alice', 50000);
console.log(employee.getDetails());

In this example, we import the `MyNamespace` namespace from the `myModule.ts` file and create instances of the `Person` interface and `Employee` class within that namespace.

Remember, when using namespaces with external modules, it's essential to compile your Typescript code correctly. You can do this by specifying the `--outFile` flag in the Typescript compiler to bundle all your external modules into a single output file.

Bash

tsc --outFile bundle.js main.ts

By following these steps, you can effectively use namespaces with Typescript external modules to keep your code organized, maintainable, and scalable. Just like a well-organized toolbox, namespaces and modules help you build robust and efficient applications. Time to pick up your virtual tools and start coding like a pro!

×