将第3方js文件与TypeScript一起使用 [英] Using a 3rd party js file with TypeScript

查看:62
本文介绍了将第3方js文件与TypeScript一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TypeScript的新手,我希望能够使用没有定义文件的第3方库.Typescript是否允许您使用外部库?

I am new to typeScript and I want to be able to use a 3rd party library that does not have definition file. Does typescript allow you to use the external libraries?

我要使用的库是filesaver.js https://github.com/eligrey/FileSaver.js/

The library i am trying to use is 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.

非常感谢!

推荐答案

打字稿允许您使用外部库吗?

Does typescript allow you to use the external libraries?

非常容易.您只需要告诉打字稿.让我们看看你的情况.

Very easily. You just need to tell typescript about it. lets look at your case.

我要使用的库是filesaver.js

The library i am trying to use is filesaver.js

仅需一个函数 saveAs .最简单的声明:

Simple just one function 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

更多精确,但样本可能过于局限:

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");

这篇关于将第3方js文件与TypeScript一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆