This project is read-only.

How to 'link' a javascript library in VS2012?

Topics: General
Dec 19, 2012 at 9:46 AM

When setting up a TypeScript project in VS and I use a folder structure like
- libs
- scr

and I put some javascript libraries in 'libs', how can I 'link' those libraries so my .ts files see these files (including the descriptions)

Dec 20, 2012 at 4:23 PM

Currently, you don't reference the .js files directly from your .ts files.  Instead, you create a .d.ts file, which lists the type information of the functionality in the .js file you want to expose to TypeScript.  You'd reference this .d.ts file in your .ts file. 

Once you compile your .ts file, you could include the output alongside the .js file you wrote the .d.ts for.  For example, having both referenced via the script tag in a web page.  The samples directory has a few examples of this, the jQuery example in the samples directory at might help.

The general philosophy is that the TypeScript just "fits in" after being compiled, so that you use it in all the same places you would use hand-written JavaScript.


Dec 26, 2012 at 11:59 AM

Got it, thanks! And do I understand correctly that if - for instance - I want to use Bootstrap, I can add the Bootstrap files in a .ts file and use the compiler to generate the .d.ts file?

Dec 28, 2012 at 6:38 PM

TypeScript can infer some typing, but generally just using a JavaScript file as a TypeScript file won't get the best results.  You can either convert the JavaScript file to a TypeScript file and any type annotations needed to make the typing usable & warning-free, or grab yourself a .d.ts file created by someone else and use this.

For some of the common libraries, there is a DefinitelyTyped project that is collecting typing files from the community.  Looks like they have a bootstrap one here:  (Disclaimer: I haven't actually tried using this, so can't attest to anything by it's existence).

 - Bill

Jan 10, 2013 at 10:24 AM

Thanks Bill! I've checked out the repository and it works like a charm.