Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling JavaScript directly from TypeScript

Tags:

typescript

I have just downloaded the TypeScript documentation. I have a some JavaScript classes and I would like to create and use these class in a TypeScript test application

How can I call a JavaScript function of an included JavaScript from TypeScript. I do not want to create a "d.ts" file. Just to create my classes, call its methods, access its properties.

How do I do that?


I am trying to use kendoUI with TypeScript.

For instance to show a window I have to do:

  1. Have a HTML to represent the content of a window. I have a div with an id logonDialog. This div
    is initially hidden;
  2. I create the window: logonDlg.kendoWindow(logOnParams);
  3. Then using jQuery I show the div: using logonDlg.show();

Example

var logonDlg = $("logonDialog");

if (!logonDlg.data("kendoWindow")) {
   logonDlg.kendoWindow(logOnParams);
   logonDlg.show();
}

It is working OK. The JS is generated as I want but I receive an error since The property 'kendoWindow' does not exist on value of type 'JQuery'.

How can I disable this kind of error. I could not make, what Ryan said, to work.

like image 881
mvbaffa Avatar asked Oct 03 '12 14:10

mvbaffa


People also ask

Can you call JavaScript from TypeScript?

You can call JavaScript functions from external files with no worries. But you have to declare them so TypeScript knows about them. If you dont, your code will work, but you will get an error while compiling.

How do I run a JavaScript file in TypeScript?

First, we read the file content of TS and use the transpileModule method provided by the typescript module to compile the TS code into JS code, get the JS code string of outputText , and finally use the native API Module. prototype. _compile provided by Node. js to compile and execute the JS code string.

Can you use TypeScript and JavaScript together?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).


3 Answers

If you want to stop the errors without doing much else extra work, you can 'declare' the objects from your JS code:

declare var w; // implicit type here is 'any' // (later, anywhere in your file...) var x = new w(); // you can do whatever you want with w now without getting errors w.x = 4; // etc. 
like image 179
Ryan Cavanaugh Avatar answered Sep 22 '22 14:09

Ryan Cavanaugh


There is a better solution. Just cast the jQuery logonDlg to any like this:

(<any>logonDlg).kendoWindow(logOnParams);

The code will be a bit different but will work the same.

  1. Without the cast th generated code is like this: logonDlg.kendoWindow(logOnParams);
  2. With the cast will be like this: (logonDlg).kendoWindow(logOnParams);

Both work OK.

Regards

like image 25
mvbaffa Avatar answered Sep 23 '22 14:09

mvbaffa


You just do it. TypeScript won't stop you. You will see warnings in the compiler output but tsc will generate your JS file just fine.

like image 26
joshuapoehls Avatar answered Sep 24 '22 14:09

joshuapoehls