What are the Modules in Typescript?
The files created in typescript have global access, which means that variables declared in one file are easily accessed in another file. This global nature can cause code conflicts and can cause issues with execution at run-time. You have export and import module functionality which can be used to avoid global variable, function conflicts. This feature is available in JavaScript with ES6 release and also supported in typescript.
Why do you need Modules in Typescript?
Following example shows the issue without modules:
Example test1.ts
let age : number = 25;
You have defined a variable age of type number in test1.ts.
Example test2.ts
In test2.ts file you are easily able to access the variable age defined in test1.ts and also modify it as shown below:
age = 30; // changed from 25 to 30. let _new_age = age;
So the above case can create a lot of problems as the variables are globally available and can be modified.
With Modules, the code written remains locale to the file and cannot be accessed outside it. To access anything from the file, it has to be exported using the export keyword. It is used when you want the variable, class, function, or interface to be used in another file. Import is used when you want to access the exported variable, class, or interface or function too. Doing so the code is written remains intact within the file, and even if you define same variable names, they are not mixed up and behave local to the file where they are declared.
Using Export and Import
There are many ways to export and import. So will discuss syntax here which is mostly used.
The syntax for import and export 1:
export nameofthevariable or class name or interface name etc //To import above variable or class name or interface you have to use import as shown below: Import {nameof thevariable or class name or interfacename} from "file path here without.ts"
Here is a working example using export and import.
Example:
test1.ts
export let age: number = 25;
Export keyword is used to share age variable in another file.
test2.ts
import { age } from "./test1" let new_age :number = age;
Import keyword is used to access the age variable, and you need to specify the file location as shown above.
Syntax for import and export 2:
There is another way to export, and import and the syntax for the same is as shown below:
export = classname; import classname = require(“file path of modulename”)
When you are using export = to export your module, the import has to use require(“file path of modulename”) to import it.
Here is a working example showing the above case:
Customer.ts
class Customer { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } getName(): string { return this.name; } } export = Customer;
testCustomer.ts
import Customer = require("./Customer"); let a = new Customer("Harry", 30); alert(a.getName());
Module Loader
Modules cannot work on its own, so you need module loader to locate the import dependencies as you have seen in examples shown above. The module loader available is CommonJS for nodejs and Require.js to run in the browser.
To compile code using CommonJS module use following command:
tsc --module commonjs testCustomer.ts
To compile code using Requirejs module use following command:
tsc --module amd testCustomer.ts
The dependent files will get converted to js file with above command.
Example testCustomer.ts to testCustomer.js using Requirejs
define(["require", "exports", "./Customer"], function (require, exports, Customer) { "use strict"; exports.__esModule = true; var a = new Customer("Harry", 30); alert(a.getName()); });
Example Customer.ts to Customer.js using Requirejs
define(["require", "exports"], function (require, exports) { "use strict"; var Customer = /** @class */ (function () { function Customer(name, age) { this.name = name; this.age = age; } Customer.prototype.getName = function () { return this.name; }; return Customer; }()); return Customer; });
To test it using require.js, you need to create a file called main.js, which has reference to the dependencies as shown.
Here is the folder structure:
src/ Customer.js testCustomer.js main.js require.js // you can get this file from github or npm install requirejs test.html
main.js
define(function (require) { var customer = require("./Customer"); var testCustomer = require("./testCustomer"); });
test.html
<!DOCTYPE html> <html> <head> <title>Typescript Module testing using Requirejs</title> <script data-main="main" src="/require.js"></script> </head> <body> <h3>Testing modules using Requirejs</h3> </body> </html>