如何将第三方Javascript文件导入到IntelliJ中,以便我可以在一个Typescript文件中引用它

我正在尝试在IntelliJ中编写Typescript,不知道如何告诉IntelliJ“导入”一些第三方的Javascript文件。 IntelliJ(或者是Node.JS?)给出了以下的抱怨:

C:/Temp/Typescript Example Project/ts/FinancialService.ts(2,17): error TS2095: Could not find symbol 'com'. C:/Temp/Typescript Example Project/ts/FinancialService.ts(4,31): error TS2095: Could not find symbol 'com'. 

我想'导入' Thirdparty.Calculator.js

 var com = com || {}; com.thirdparty = com.thirdparty || {}; com.thirdparty.Calculator = function() { this.add = function(a, b) { return a + b; }; this.square = function(n) { return n*n; }; }; 

这就是FinancialService.ts的外观:

 class FinancialService { calculator: com.thirdparty.Calculator; constructor() { this.calculator = new com.thirdparty.Calculator(); } calculateStuff(a: number) { return this.calculator.square(a); } } 

IntelliJ似乎能够传输Typescript,如下所示,并将正确的值logging到控制台:

 <html> <head> <script src="js/Thirdparty.Calculator.js"></script> <script src="ts/FinancialService.js"></script> <script> var cal = new com.thirdparty.Calculator(); console.log("Calculator.square() is " + cal.square(9)); var fs = new FinancialService(); console.log("FinancialService.calculateStuff() is " + fs.calculateStuff(4)); </script> </head> <body> </body> </html> 

我怎样才能configuration我的项目,使IntelliJ知道Thirdparty.Calculator.js

您可以将Thirdparty.Calculator.d.ts添加到您的项目以进行TypeScript编译:

 declare module com.thirdparty { export class Calculator { add(a: number, b: number) : number; square(n: number) : number; } } 

这显然需要与第三方图书馆一起成长。

只需要额外的努力,你可以把它转换成TypeScript …

 module com.thirdparty { export class Calculator { add = function(a: number, b: number) { return a + b; }; square(n: number) : number { return n*n; } } }