Open In App

Module Augmentation in TypeScript

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Module Augmentation is a feature of TypeScript that extends or modifies existing modules without making direct changes in the original code. This is very useful when we are handling third-party libraries that cannot be modified. We can use module augmentation to add new functionality to an existing module, making it more versatile.

What is Module Augmentation?

Module Augmentation allows us to add new functionality to existing modules. We can extend libraries or add new functionalities modules that are written by any other developer without changing the actual code of the library. This is commonly used with third-party libraries. Suppose, we are using a library and need to add more functions or features that were not initially present or provided in it, we can use module augmentation to do so in our own project without changing the library itself.

In TypeScript, modules which are also known as namespaces can be augmented by reopening the module and adding new types, functions, or variables. This concept is the same as extending classes or adding methods to prototypes in JavaScript.

Step-by-Step Implementation of Module Augmentation

Step 1: Create the Project in VS Code

  • Create a new folder for your project. You can name it something like module-augmentation.
  • Now, we initialize a new Node.js project in your folder by running the following command in the terminal:
npm init -y

Step 2: Install TypeScript

npm install typescript --save-dev

Step 3: Configure a TypeScript Configuration File

npx tsc --init

Step 4: Create TypeScript Files

  • Now, we create the files required to demonstrate module augmentation.
  • First create a math-lib-augmentation.d.ts file: This file will hold our module augmentation code. In this file, we'll extend an imaginary module called math-lib.
  • We add the following code to math-lib-augmentation.d.ts:
declare module 'math-lib' {
export function subtract(x: number, y: number): number;
}

Step 5: Create an app.ts file

  • This is our main TypeScript file where you will use the augmented module.
  • We add the following code to app.ts:
import { subtract } from './math-lib';  
const difference = subtract(10, 4);
console.log('Difference:', difference);

Step 6: Create a math-lib.ts file

  • Since math-lib is a hypothetical library, we will create our own subtract function.
  • Now, add the following code to math-lib.ts:
export function subtract(x: number, y: number): number {
return x - y;
}

Project Structure:

project-fold
Project-Structure

Example: In this example we will use the Module Augmentation

JavaScript
// app.ts

import { subtract } from './math-lib'
const difference = subtract(10, 4);
console.log('Difference:', difference);
JavaScript
//   math-lib-augmentation.d.ts

declare module 'math-lib' {
    export function subtract(x: number, y: number): number;
  }
JavaScript
//   math-lib.ts

export function subtract(x: number, y: number): number {
    return x - y;
  }

Step 7: Compile and Run the Code

Compile TypeScript: In the terminal, we type the following command to compile our TypeScript files into JavaScript:

npx tsc

This command will convert our .ts files into .js files, which you can run using Node.js.

Step 8: Run the JavaScript Code

node app.js

Output:

Difference: 6

Why Use Module Augmentation?

  • It allows us to extend the functionality of existing modules without making changes in the source code.
  • It keeps our augmentations separate from the original module, we can make our code easier to maintain.

Conclusion

Module augmentation is a useful feature in TypeScript that is used to extend existing modules in a very clean way. It is useful when we deal with third-party libraries or legacy code. We have seen a simple example above which shows how o augment a simple module and use it within your TypeScript project.


Next Article

Similar Reads