How to use javascript in typescript

前端 未结 2 1183
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 09:17

I have a javascript file called ui.js.

Inside ui.js is UI setup code.

if (!(\"ontouchstart\" in document.documentElement)) {
    document.documentEle         


        
相关标签:
2条回答
  • 2020-12-09 10:00

    But I have not been able to figure out how to do that.

    Simply set allowJs to true in your tsconfig.json compilerOptions and then make sure the .js file is included using files/include/exclude etc.

    More

    I did a video on the subject as well https://www.youtube.com/watch?v=gmKXXI_ck7w

    0 讨论(0)
  • 2020-12-09 10:23

    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.

    You can use an Anonymously-typed var:

    declare var myFunction;

    or an Interfaced-typed var:

    interface __myFunction { } declare var myFunction: __myFunction;

    You may also write this into a declaration file(but its not required). See also TypeScript documentation.

    0 讨论(0)
提交回复
热议问题