Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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/

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!

like image 447
ChrisSmathers Avatar asked Oct 21 '25 06:10

ChrisSmathers


1 Answers

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

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

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");
like image 142
basarat Avatar answered Oct 23 '25 18:10

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!