TypeScript module

TypeScript module:

A module refers to a set of standardized parts or independent units that can be used to construct a more complex structure. TypeScript modules provides a way to organize the code for better reuse.

Syntax:

export interface InterfaceName { 
   //Block of statements 
}

The import keyword is used to use the declared module in another file.

import testInterfaceRef = require(“./InterfaceName”);

Example:

IShowDetails.ts

export interface IShowDetails { 
   display(); 
}

Student.ts

import showDetails = require("./IShowDetails"); 
export class Student implements showDetails.IShowDetails { 
   public display() { 
      console.log("Student Details"); 
   } 
} 

Teacher.ts

import showDetails = require("./IShowDetails"); 
export class Teacher implements showDetails.IShowDetails { 
   public display() { 
      console.log("Teacher details."); 
   } 
}

TestShape.ts

import showDetails = require("./IShowDetails"); 
import student = require("./Student"); 
import teacher = require("./Teacher");  

function showAllDetails(detailsToShow: showDetails.IShowDetails) {
   detailsToShow.display(); 
} 

showAllDetails(new student.Student()); 
showAllDetails(new teacher.Teacher());