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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export interface InterfaceName {
//Block of statements
}
export interface InterfaceName { //Block of statements }
export interface InterfaceName { 
   //Block of statements 
}

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import testInterfaceRef = require(“./InterfaceName”);
import testInterfaceRef = require(“./InterfaceName”);
import testInterfaceRef = require(“./InterfaceName”);

Example:

IShowDetails.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
export interface IShowDetails {
display();
}
export interface IShowDetails { display(); }
export interface IShowDetails { 
   display(); 
}

Student.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import showDetails = require("./IShowDetails");
export class Student implements showDetails.IShowDetails {
public display() {
console.log("Student Details");
}
}
import showDetails = require("./IShowDetails"); export class Student implements showDetails.IShowDetails { public display() { console.log("Student Details"); } }
import showDetails = require("./IShowDetails"); 
export class Student implements showDetails.IShowDetails { 
   public display() { 
      console.log("Student Details"); 
   } 
} 

Teacher.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import showDetails = require("./IShowDetails");
export class Teacher implements showDetails.IShowDetails {
public display() {
console.log("Teacher details.");
}
}
import showDetails = require("./IShowDetails"); export class Teacher implements showDetails.IShowDetails { public display() { console.log("Teacher details."); } }
import showDetails = require("./IShowDetails"); 
export class Teacher implements showDetails.IShowDetails { 
   public display() { 
      console.log("Teacher details."); 
   } 
}

TestShape.ts

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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());
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());
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());