GAZAR

Principal Engineer | Mentor

Modules in TypeScript: A Comprehensive Guide

Modules in TypeScript: A Comprehensive Guide

A module is a self-contained piece of code that can be used to organize and reuse code. Modules are a way to break down a large program into smaller, more manageable pieces. In TypeScript, modules are defined using the module keyword.

module MyModule {
  export function sayHello(name: string) {
    console.log(`Hello, ${name}!`);
  }
}

To use a module in TypeScript, you can import it into your code using the import statement. For example:

import { sayHello } from './MyModule';
sayHello('John');

In this article, we have explored the concept of modules in TypeScript and provided examples to illustrate how they work. Modules are a powerful tool that can be used to organize and reuse code. By using modules, you can write more efficient and effective code that is easier to maintain and update.