Welcome Guest. Sign in or Signup


How to use external js in typescript?

Asked by: 19916 views 5.TypeScript


Not everything you reference in your TypeScript code has to be defined in TypeScript itself. You can reference JavaScript code that is defined in some JS file using the “declare” keyword as seen in the example below.

// File Base.js
var piValue = 3.14;
function getCircleArea(radius) {
    return piValue * radius * radius;
// End of file

// File file1.ts
declare function getCircleArea(radius);

module Module1.Calculator {
    export class Calc {
        getArea(radius) {
            // Accessing the Base.js file method
            var area = getCircleArea(radius);
            console.log("Area: " + area);
            return area;
// End of file

// In sample
// "Base.js" is must to reference before the "file1.js" file
<script src="Base.js"></script>
<script src="file1.js"></script>

   var calc = new Module1.Calculator.Calc(12);
   var area = calc.getArea(calc.x);
   alert("Calculated Area is " + area);

Here is the resultant view of sample,
Using the external js in TypeScript



  1. click here on Dec 18, 2014 Reply

    Thanks-a-mundo for the post.Thanks Again.

Comments on Question