- 4 years ago
- Afaq Arif
- 3,317 Views
-
2
In this chapter, we will study about namespaces in TypeScript with examples.
A namespace is a path to logically group related code. It is inbuilt into TypeScript. In JavaScript, you need to state a variable into a global range. You can also face the “global namespace pollution problem” in JavaScript. It is due to the use of multiple javaScript files within the same program, with the possibility of overwriting the same variables.
Defining a Namespace
A namespace definition starts with the keyword namespace followed by the namespace name as follows.
namespace SomeNameSpaceName {
export interface ISomeInterfaceName { }
export class SomeClassName { }
}
The classes or interfaces which should be entered outside the namespace should be marked with keyword export.
The syntax will be namespaceName.className, when you enter the class or interface in another namespace.
SomeNameSpaceName.SomeClassName;
If the first namespace is in separate TypeScript file, then it should be referenced using triple slash reference syntax.
/// <reference path = "SomeFileName.ts" />
The following program shows use of namespaces.
FileName :IShape.ts
----------
namespace Drawing {
export interface IShape {
draw();
}
}
FileName :Circle.ts
----------
/// <reference path = "IShape.ts" />
namespace Drawing {
export class Circle implements IShape {
public draw() {
console.log("Circle is drawn");
}
FileName :Triangle.ts
----------
/// <reference path = "IShape.ts" />
namespace Drawing {
export class Triangle implements IShape {
public draw() {
console.log("Triangle is drawn");
}
}
FileName : TestShape.ts
/// <reference path = "IShape.ts" />
/// <reference path = "Circle.ts" />
/// <reference path = "Triangle.ts" />
function drawAllShapes(shape:Drawing.IShape) {
shape.draw();
}
drawAllShapes(new Drawing.Circle());
drawAllShapes(new Drawing.Triangle());
}
}
}
The above code can be composed and run using the following command.
tsc --out app.js TestShape.ts
node app.js
The following JavaScript code(app.js) shows on composing.
//Generated by typescript 1.8.10
/// <reference path = "IShape.ts" />
var Drawing;
(function (Drawing) {
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn");
};
return Circle;
}());
Drawing.Circle = Circle;
})(Drawing || (Drawing = {}));
/// <reference path = "IShape.ts" />
var Drawing;
(function (Drawing) {
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn");
};
return Triangle;
}());
Drawing.Triangle = Triangle;
})(Drawing || (Drawing = {}));
/// <reference path = "IShape.ts" />
/// <reference path = "Circle.ts" />
/// <reference path = "Triangle.ts" />
function drawAllShapes(shape) {
shape.draw();
}
drawAllShapes(new Drawing.Circle());
drawAllShapes(new Drawing.Triangle());
The output of the above code is as follows.
Circle is drawn
Triangle is drawn
Nested Namespaces
We can define one namespace inside another namespace as follows.
namespace namespace_name1 {
export namespace namespace_name2 {
export class class_name { }
}
}
You can enter members of nested namespace by using the dot (.) operator as follows.
FileName : Invoice.ts
namespace tutorialPoint {
export namespace invoiceApp {
export class Invoice {
public calculateDiscount(price: number) {
return price * .40;
}
}
}
}
FileName: InvoiceTest.ts
/// <reference path = "Invoice.ts" />
var invoice = new tutorialPoint.invoiceApp.Invoice();
console.log(invoice.calculateDiscount(500));
You can compose and run the above code using the following command,
tsc --out app.js InvoiceTest.ts
node app.js
It shows following JavaScript code(app.js) on composing.
//Generated by typescript 1.8.10
var tutorialPoint;
(function (tutorialPoint) {
var invoiceApp;
(function (invoiceApp) {
var Invoice = (function () {
function Invoice() {
}
Invoice.prototype.calculateDiscount = function (price) {
return price * .40;
};
return Invoice;
}());
invoiceApp.Invoice = Invoice;
})(invoiceApp = tutorialPoint.invoiceApp || (tutorialPoint.invoiceApp = {}));
})(tutorialPoint || (tutorialPoint = {}));
/// <reference path = "Invoice.ts" />
var invoice = new tutorialPoint.invoiceApp.Invoice();
console.log(invoice.calculateDiscount(500));
The output of the above code is as follows.
- 4 years ago
- Afaq Arif
- 3,317 Views
-
2