Open In App

Difference Between Internal & External Modules in TypeScript

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, modules are a way to organize code and encapsulate functionality. There are two types of modules: internal modules (also known as namespaces) and external modules (also known as modules).

Internal Modules (Namespaces):

Internal modules, known as namespaces, provide a way to organize code within a single global scope. They help in avoiding naming conflicts by encapsulating code within a named container.

Syntax:

namespace MyNamespace {
// code within the namespace
}

Example: Here, a namespace named MathOperations is created to group mathematical functions like addition and subtraction. The functions are then accessed within the namespace, showcasing the encapsulation provided by internal modules.

Javascript




namespace MathOperations {
    export function add(x: number, y: number): number {
        return x + y;
    }
 
    export function subtract(x: number, y: number): number {
        return x - y;
    }
}
 
// Usage
let resultAdd = MathOperations.add(5, 3);
let resultSubtract = MathOperations.subtract(8, 2);
 
console.log(resultAdd);      // Output: 8
console.log(resultSubtract); // Output: 6


Output:

8
6

External Modules

External modules allow you to organize code into separate files. Each file is treated as a separate module, and you can explicitly export and import functionalities between these modules. It is also known as the ES6 module.

Syntax: File 1 (moduleA.ts):

// moduleA.ts
export const variableA = 10;
export function functionA(): void {
// code
}

File 2 (moduleB.ts):

// moduleB.ts
import { variableA, functionA } from './moduleA';
// code using variableA and functionA

Example: Here, moduleA.ts defines a namespace MathOperations, and moduleB.ts imports and uses the functionalities from moduleA.

Javascript




// moduleA.ts
export namespace MathOperations {
    export function add(x: number, y: number): number {
        return x + y;
    }
 
    export function subtract(x: number, y: number): number {
        return x - y;
    }
}


Javascript




// moduleB.ts
import { MathOperations } from './moduleA';
 
// Usage
let resultAdd = MathOperations.add(5, 3);
let resultSubtract = MathOperations.subtract(8, 2);
 
console.log(resultAdd);      // Output: 8
console.log(resultSubtract); // Output: 6


Output:

8
6


Similar Reads

Difference between Inline, Internal and External CSS
CSS is used to add styles on web pages that contain HTML elements. There are three methods to add styles on web pages, these are - Inline, Internal, and External. In this article, we will see the differences between Inline, Internal, and External CSS styles. Table of Content Inline CSSInternal CSSExternal CSSInline, Internal and External CSS Use Ca
5 min read
What are the Differences between Internal DTD and External DTD ?
DTD stands for Document Type Definition, and it is used to define the structure and content of an XML document. An XML document can have an internal DTD or an external DTD, depending on the needs of the user. In this article, we will discuss the differences between internal and external DTDs. This article will discuss the dissimilarities between th
4 min read
How to export modules with sub-modules in ReactJS ?
One of the key features of React is its modular structure, which allows developers to break down their code into reusable components. In many cases, these components are organized into modules and submodules, which can be exported and imported into other parts of the application. Prerequisites:NPM & Node.jsReact JSReact JS ModulesWhat are Modul
3 min read
How to use External Modules and NPM in a project ?
Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into multiple files. Node.js adopts this CommonJS forma
3 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
What are the Modules in Typescript ?
In this article, we talk about what types of modules are introduced by TypeScript. Before we discuss the types of modules we will see what the module concept is. Let's see. When ES6 introduced the concept of the module, JavaScript started supporting the module concepts as a part of the JavaScript library. When TypeScript comes into the picture it a
5 min read
How to import/export Modules in TypeScript ?
Modules are a way of organizing and structuring the code into reusable and maintainable code blocks which can be imported into any other file where we need the same kind of code. It enhances the reusability, maintainability, and readability of our code which helps to improve the web performance of the website. You can import and export the TypeScri
1 min read
Differece between JavaScript Modules & NgModule
Modules play an important role in structuring and organizing the code of Javascript and Angular.js. They help us to use the code efficiently. Majorly NgModule in Angular.js and Modules in Javascript are widely used in different scenarios. In this article, We will learn about Modules and their importance in Angular.js and Javascript. Table of Conten
5 min read
How to define the relationship between a document and the external resources?
In this article, we learn how to create the relationship between a document and the external resources by using the <link> Element. This tag is used to define a link between a document and an external resource. The link tag is used to link to external style sheets. This element can appear multiple times but it goes only in the head section. T
1 min read
How to remove all Global Modules in Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that Node.js is not a framework and it’s not a programming language. Most of the people are confused and understand it’s a framework or a programming language. We often use Node.js for building back-end services
2 min read
Article Tags :