Importing JS File into Typescript

前端 未结 2 1280

I\'m looking at moving over to Typescript and currently looking at doing this slowly if possible file by file.

Now the system I currently have is built with Webpack and

相关标签:
2条回答
  • 2021-02-08 01:39

    Any ideas I have searched quite a lot but I can't really make sense of importing JS files into Typescript.

    For webpack all you need is to declare the require function and do what you are already used to:

    var foo = require('../../../../vendor/src/foo');
    

    The require function type definition is present here : https://github.com/TypeStrong/ts-loader#loading-other-resources-and-code-splitting

    If you want to use foo in a stronger manner... recommend porting it over to .ts as well instead of struggling to build and then maintain a .d.ts file for it.

    0 讨论(0)
  • 2021-02-08 01:48

    What about simply rename foo.js to foo.ts (it may require some additional steps but often it works fine) and then rewrite your snippet to:

     /// <reference path="../../../../vendor/src/foo.d.ts" />
    
     foo('something');
    
    0 讨论(0)
提交回复
热议问题