How to use javascript in typescript

I have a javascript file called ui.js.

Inside ui.js is the UI installation code.

if (!("ontouchstart" in document.documentElement)) { document.documentElement.className += " no-touch"; var jScrollOptions = { autoReinitialise: true, autoReinitialiseDelay: 100 }; $('.box-typical-body').jScrollPane(jScrollOptions); $('.side-menu').jScrollPane(jScrollOptions); $('.scrollable-block').jScrollPane(jScrollOptions); } 

}

I would like to be able to call this from typescript.

I do not want to convert the code to typescript, since there are hundreds of lines, and this is really not necessary. It just needs to be once after the user interface is ready.

It seems to me that I should be able to wrap it in a function and then call this function from typescript.

But I could not figure out how to do this.

Note. This is not a duplicate of an earlier question, as it was how to convert js, and not use it directly with minimal changes.

+6
source share
2 answers

But I could not figure out how to do this.

Just set allowJs to true in tsconfig.json compilerOptions , and then make sure the .js file is included with files / include / exclude , etc.

More details

I made a video on this, as well as https://www.youtube.com/watch?v=gmKXXI_ck7w

+10
source

You can call JavaScript functions from external files without any problems. But you have to declare them, so TypeScript knows about them. If you do not, your code will work, but when compiling you will receive a message.

You can use an anonymously typed var:

declare var myFunction;

or mixed var type:

interface __myFunction { } declare var myFunction: __myFunction;

You can also write this to the declaration file (but not required). See Also TypeScript Documentation .

+3
source

All Articles