TypeScript Namespaces
The clearest purpose of namespaces is to solve the problem of duplicate names.
Consider a situation where a class has two students named Xiaoming. To clearly distinguish them, in addition to their given name, we have to use some extra information, such as their surnames (Wang Xiaoming, Li Xiaoming), or their parents' names, and so on.
A namespace defines the visibility scope of identifiers. An identifier can be defined in multiple namespaces, and its meaning in different namespaces is unrelated. Thus, any identifier can be defined in a new namespace without conflicting with any existing identifiers, because existing definitions are all in other namespaces.
In TypeScript, namespaces are defined using namespace, with the following syntax:
namespace
SomeNameSpaceName
{
export
interface
ISomeInterfaceName
{
}
export
class
SomeClassName
{
}
}
The above defines a namespace SomeNameSpaceName. If we need to call the classes and interfaces inside SomeNameSpaceName from outside, we need to add the export keyword to those classes and interfaces.
To call it from another namespace, the syntax is:
SomeNameSpaceName.SomeClassName;
If a namespace is in a separate TypeScript file, it should be referenced using a triple-slash ///, with the following syntax:
///
The following example demonstrates the use of namespaces defined in different files:
Code in IShape.ts:
namespace
Drawing
{
export
interface
IShape
{
draw
(
)
;
}
}
Code in Circle.ts:
//
/
namespace
Drawing
{
export
class
Circle
implements
IShape
{
public
draw
(
)
{
console
.
log
(
"
Circle is drawn
"
)
;
}
}
}
Code in Triangle.ts:
//
/
namespace
Drawing
{
export
class
Triangle
implements
IShape
{
public
draw
(
)
{
console
.
log
(
"
Triangle is drawn
"
)
;
}
}
}
Code in TestShape.ts:
//
/
//
/
//
/
function
drawAllShapes
(
shape
:
Drawing
.
IShape
)
{
shape
.
draw
(
)
;
}
drawAllShapes
(
new
Drawing
.
Circle
(
)
)
;
drawAllShapes
(
new
Drawing
.
Triangle
(
)
)
;
Compile the above code using the tsc command:
tsc --out app.js TestShape.ts
The following JavaScript code is obtained:
JavaScript
//
/
var
Drawing
;
(
function
(
Drawing
)
{
var
Circle
=
/*
- @class
*/
(
function
(
)
{
function
Circle
(
)
{
}
Circle
.
prototype
.
draw
=
function
(
)
{
console
.
log
(
"
Circle is drawn
"
)
;
}
;
return
Circle
;
}
(
)
)
;
Drawing
.
Circle
=
Circle
;
}
)
(
Drawing
||
(
Drawing
=
{
}
)
)
;
//
/
var
Drawing
;
(
function
(
Drawing
)
{
var
Triangle
=
/*
- @class
*/
(
function
(
)
{
function
Triangle
(
)
{
}
Triangle
.
prototype
.
draw
=
function
(
)
{
console
.
log
(
"
Triangle is drawn
"
)
;
}
;
return
Triangle
;
}
(
)
)
;
Drawing
.
Triangle
=
Triangle
;
}
)
(
Drawing
||
(
Drawing
=
{
}
)
)
;
//
/
//
/
//
/
function
drawAllShapes
(
shape
)
{
shape
.
draw
(
)
;
}
drawAllShapes
(
new
Drawing
.
Circle
(
)
)
;
drawAllShapes
(
new
Drawing
.
Triangle
(
)
)
;
Use the node command to view the output:
$ node app.js
Circle is drawn
Triangle is drawn
Nested namespaces
Namespaces support nesting, meaning you can define a namespace inside another namespace.
namespace
namespace_name1
{
export
namespace
namespace_name2
{
export
class
class_name
{
}
}
}
Members are accessed using the dot ., as in the following example:
Code in Invoice.ts:
namespace
Runoob
{
export
namespace
invoiceApp
{
export
class
Invoice
{
public
calculateDiscount
(
price
:
number
)
{
return
price
.40
;
}
}
}
}
Code in InvoiceTest.ts:
//
/
var
invoice
=
new
Runoob
.
invoiceApp
.
Invoice
(
)
;
console
.
log
(
invoice
.
calculateDiscount
(
500
)
)
;
Compile the above code using the tsc command:
tsc --out app.js InvoiceTest.ts
The following JavaScript code is obtained:
JavaScript
var
Runoob
;
(
function
(
Runoob
)
{
var
invoiceApp
;
(
function
(
invoiceApp
)
{
var
Invoice
=
/*
- @class
*/
(
function
(
)
{
function
Invoice
(
)
{
}
Invoice
.
prototype
.
calculateDiscount
=
function
(
price
)
{
return
price
.40
;
}
;
return
Invoice
;
}
(
)
)
;
invoiceApp
.
Invoice
=
Invoice
;
}
)
(
invoiceApp
=
Runoob
.
invoiceApp
||
(
Runoob
.
invoiceApp
=
{
}
)
)
;
}
)
(
Runoob
||
(
Runoob
=
{
}
)
)
;
//
/
var
invoice
=
new
Runoob
.
invoiceApp
.
Invoice
(
)
;
console
.
log
(
invoice
.
calculateDiscount
(
500
)
)
;
Use the node command to view the output:
$ node app.js
200