简体   繁体   English

使用TypeScript的第三方js文件

[英]Using a 3rd party js file with TypeScript

I am new to typeScript and I want to be able to use a 3rd party library that does not have definition file. 我是typeScript的新手,我希望能够使用没有定义文件的第三方库。 Does typescript allow you to use the external libraries? 打字稿是否允许您使用外部库?

The library i am trying to use is filesaver.js https://github.com/eligrey/FileSaver.js/ 我试图使用的库是filesaver.js https://github.com/eligrey/FileSaver.js/

Do I need to create a definition file for this library? 我是否需要为此库创建定义文件?

I just need someone to point me in the right direction. 我只需要有人指出我正确的方向。

thanks so much! 非常感谢!

Does typescript allow you to use the external libraries? 打字稿是否允许您使用外部库?

Very easily. 非常简单地。 You just need to tell typescript about it. 你只需要告诉打字稿。 lets look at your case. 让我们来看看你的情况。

The library i am trying to use is filesaver.js 我想使用的库是filesaver.js

Simple just one function saveAs . 简单只需一个函数saveAs The simplest declaration: 最简单的声明:

declare var saveAs:any; 

and now the following will compile just fine: 现在以下将编译得很好:

declare var saveAs:any; 
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

To write more advanced declrations take a look at: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/declaration%20files/Introduction.md 要编写更高级的declrations,请查看: https//github.com/Microsoft/TypeScript-Handbook/blob/master/pages/declaration%20files/Introduction.md

Note 注意

A more exact but possibly overly restrictive sample : 准确但可能过于严格的样本:

declare function saveAs(data:Blob , filename:string );
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

Another way to call external libraries in typescript without compiling error is to use the window"myglobalfunction" notation. 另一种在打字稿中调用外部库而不编译错误的方法是使用窗口“myglobalfunction”表示法。

For example: 例如:

jQuery call: window["$"]
fileSaver.js : window["saveAs"](blob, "hello world.txt");

etc... 等等...

These calls inside typescript don't generate compilation errors, but they are full functioning ways to call the same standard functions. 打字稿中的这些调用不会产生编译错误,但它们是调用相同标准函数的全功能方法。

Out of the pragmatic but perhaps not quite kosher categoy, a less elegant and non-TypeScript approach would be to simply declare but not assign the variable/function you want to use with TypeScript. 出于实用但可能不太合适的类型,不太优雅且非TypeScript的方法是简单地声明但不分配要与TypeScript一起使用的变量/函数。 This does not give you Intellisense, but it does allow you to very quickly use the library without creating any declarations or roll your own d.ts file. 不会给你Intellisense,但它确实允许你快速使用库而不创建任何声明或滚动你自己的d.ts文件。

For instance, here's an Angular example to provide OidcTokenManager as a constant on an app.core module: 例如,这是一个Angular示例,在app.core模块上提供OidcTokenManager作为常量:

((): void => {
    angular
        .module('app.core')
        .constant('OidcTokenManager', OidcTokenManager);

})();

This will generate a TS2304 - Cannot find name 'OidcTokenManager' TypeScript error. 这将生成TS2304 - 找不到名称'OidcTokenManager'TypeScript错误。

However, by simply declaring OidcTokenManager as of type any , TypeScript will let you pass: 然而,通过简单地声明OidcTokenManager任何类型的,打字稿会让你通过:

((): void => {    
    let OidcTokenManager: any;   

    angular
        .module('app.core')
        .constant('OidcTokenManager', OidcTokenManager);

})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM