TypaScript Modules:

The typescript module is to manage the code in some groups in an organized way.

There are 2 types of modules in typeScript and they are:

  1. Internal Module and
  2. External Module

Internal Module:

The internal module is if you want to keep the module and its uses in the same file then it can be said internal module.
To declare an internal module first use the keyword module then the module name and then create the block of the module by using the opening and closing of the curly bracket. and inside the module declare any function or interface or classes but you have to export the functions or interfaces or classes by using the export keyword. The internal modules are used in the previous version of typescript however it’s supported by the current version of typescript. Instead of using an internal module, a TypeScript Developer should use namespace.

module moduleName{
	export function functionName(){
		console.log('Its a sample TypeScript internal module');

External Module

If you want to break a typescript file into multiple files according to the classes or interface then you can do that by using modules of typescript and it’s called an external module.
You can export the classes or interfaces from one file and can import that to another file.
Modules are not executed on a global scope, they are executed within their own scope, That is the classes, interfaces, etc declared in a module are not visible or extensible from outside of the module unless the classes or interfaces are exported from the module using keyword export. And in the file where we are going to use the content of the module that is the classes or interfaces of the module we have to import the module file in the file where we are going to use the module.

Let us understand that with an example: suppose there is an interface of student info and a class of student and from a seperate file we want to access the class and interface so for that we are going to create 3 seperate files one for the student info interface where we have to export that and another one for student class and there also we have to export the student class and another file where we are going to import the two file’s content and use the class and interface.

Example of studentInfo.ts
export interface StudentInfo{
	Name: string,
	RollNo: number

Example of student.ts
export class Student{
	construnter(private first_name, private last_name){}
		let full_name = this.first_name + ' ' + this.last_name;
		return full_name;

while we are importing a module in a TypeScript file we have to write the keyword import and then the exported class name or interface name from the module surrounded by curly bracket and then keyword from and then surrounded by single or double quotation the path of the module file but here we should exclude the .ts extension of the module

Example of app.ts
import {StudentInfo} from './studentInfo';
import {Student} from './student';

//Students Details
let istudent:StudentInfo = {
	Name: 'Code Mystery',
	RollNo: 1

let std = new Student('Code', 'Mystery');
let students_full_nm = std.getFullNm();

Now you can compile the file using the AMD system by using the below command
$ tsc –module amd app.ts

Here the app.ts is the main file name where the modules are imported.

Share The Post On -