Difference Between Internal & External Modules in TypeScript
Last Updated :
28 Apr, 2025
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 interfaces and classes in TypeScript In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript. Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same da
3 min read
What's the Difference Between 'extends' and 'implements' in TypeScript ? TypeScript offers powerful way for organizing code and managing relationships between different components through the use of extends and implements keywords. This article explores the distinctions and functionalities of these two keywords.ExtendsThe extends keyword is used for two main purposes in
2 min read
What is the difference between interface and type in TypeScript ? In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.Type in TypeScriptThe
3 min read
What is the difference between 'String' and 'string' in TypeScript ? Unlike JavaScript, TypeScript uses static typing, i.e. it specifies what kind of data the variable will be able to hold. Since TypeScript is a superscript of JavaScript, it also holds a distinction between a string and String. The usage of a string object in JS (or TS for that matter) is very minima
4 min read
Difference between Flow and TypeScript 1. Flow : Flow is developed and maintained by Facebook. It is a static type checker, designed to quickly find errors in JavaScript applications. Nothing more, nothing less. It's not a compiler, but a checker. It can work without any type of annotations and it is very good at inferring types. To enab
2 min read