#Typescript Training
Demonstration of how use module and consume external library in Typescript. See the typescript handbook for more information.
##Internal Module example
Mdl1.ts
is a typical internal moduleMdl1bis.ts
extend theMdl1.ts
using the merging feature of Typescript. It references to the originalMdl1.ts
as following
///<reference path="Mdl1.ts"/>
- The test
Mdl1Consumer.ts
wants to use all functionality ofMdl1
so it references to theMdl1bis.ts
///<reference path="Mdl1bis.ts"/>
To run the test, we must to compile and concatenate all the references into 1 file (all.js
) with the --out
option
$ tsc --out all.js Mdl1Consumer.ts
Then you can execute
$ node all.js
Output:
Hello Java
3
Great Script
##External Module example
Use top-level export
to define a external module
Mdl2.ts
is a typical external moduleMdl3.ts
add namespace level to the module- The test
Consumer.ts
is compiled and run normally, it gives the following output:
$ node Consumer.js
Hello external TOTO
Hello external with namespace Mdl3 TUTU
##External library example
- File
*.d.ts
defines an Ambient Module (which contains only declaration, without any implementation) - Ambient module describes the shape of external library
- if the external library is load through a script tag, use Ambient Internal Module
- Use Ambient External Module with quoted name to describe a nodejs modules
###Ambient Internal Module
extlib1.js
is a external libraryextlib1.d.ts
is an ambient module to describe the shape of this library- The typescript test
ConsumerExtlib1.ts
consume theextlib1.js
library - The library
extlib1.js
is loaded in thescript
tag ofindex.html
<script language="javascript" src="extlib1.js"></script>
###Ambient External Module
nodelib.d.ts
describes the shape of a nodejs module (it's thepath
module in this example)- The test
ConsumerNodelib.ts
must to reference to the ambient declaration file and import the module:
///<reference path="nodelib.d.ts"/>
import path=require("path");
The execution output:
$ node ConsumerNodelib.js
a\b
.txt
See also:
##Conclusion
We can easily organize our typescript project into modules and consume other javascript library (which are not wrote in Typescript)