Skip to content

Demonstration of how use module and consume external library in Typescript.

Notifications You must be signed in to change notification settings

duongphuhiep/learn-typescript-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Typescript Training

Demonstration of how use module and consume external library in Typescript. See the typescript handbook for more information.

##Internal Module example

	///<reference path="Mdl1.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 module
  • Mdl3.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

	<script language="javascript" src="extlib1.js"></script>

###Ambient External 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)

About

Demonstration of how use module and consume external library in Typescript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published