Easy learning with example program codes

TypeScript ambient module tutorial

TypeScript ambient module:

As we discussed in earlier tutorials TypeScript provides the facility to safely and easily use existing JavaScript libraries like jquery, angularjs, nodejs etc. Ambient declarations allow us to safely use existing popular JavaScript libraries.

Ambient declaration files are saved with .d.ts extension.

Syntax to declare ambient variables or modules:

declare module ModuleName {

Syntax to use Ambient files:

/// <reference path = "AmbientFileName.d.ts" />


Let us consider that we are using a third party js library with following code.

var TestVar;  
(function (TestVar) {  
   var Calc = (function () { 
      function Calc() { 
      Calc.prototype.doSum = function (num1, num2) {
         return num1 + num2;

As this is a js file and we still want to use the doSum() function in our TypeScript code with type safety. We can use this by using ambient declaration. Let us create an ambient declaration file.

declare module TestVar { 
   export class Calc { 
      doSum(num1:number, num2:number) : number; 

Now we have to include the above ambient declaration into our typescript file.

/// <reference path = "Calc.d.ts" /> 
var obj = new TestVar.Calc(); 

TypeScript Tutorial

TypeScript Overview.
Typescript vs Javascript.
Typescript let vs var.
TypeScript Hello World.
Typescript Data Types.
Typescript Variable Declaration.
TypeScript operators list.
TypeScript Arithmetic Operators.
TypeScript Comparison Operators.
TypeScript Bitwise Operators.
Typescript Logical Operators.
TypeScript Assignment Operators.
Typescript Conditional Operator.
Typescript String Operator.
Typescript typeof Operator.
TypeScript control statements.
TypeScript Switch Statement.
TypeScript for loop.
TypeScript for in loop.
TypeScript while loop.
TypeScript do while loop.
Typescript for loop break.
TypeScript for loop continue.
Typescript function.
Typescript returning function.
Typescript parameterized function.
Typescript function optional parameter.
Typescript rest parameters.
Typescript default parameters.
Typescript anonymous function.
Typescript function constructor.
Typescript Arrow/Lambda function.
Typescript function overloading.
TypeScript number object.
TypeScript string object.
TypeScript boolean object.
TypeScript date object.
TypeScript math object.
TypeScript array object.
TypeScript tuples.
TypeScript union type.
TypeScript interface.
TypeScript interface inheritance.
TypeScript interface multiple inheritance.
TypeScript class.
Typescript class inheritance.
Typescript class interface.
TypeScript abstract class.
TypeScript object.
TypeScript duck typing.
TypeScript NameSpace.
TypeScript Multi-file namespaces.
TypeScript module.
TypeScript ambient module.

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki