I've looked all over the internets but I have yet to find a comprehensive guide that tells me how to take a library such as jquery and use it in a TypeScript project. If there is a guide that exists I would love to know where otherwise these are the things I really need to know:
requires
or a //reference
when using VS2013 and if so what is that actually doing?I've been able to figure pretty much everything else out but this one has been rather frustrating.
The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.
JQuery is one of the most popular JavaScript libraries which exposes a lot of usable APIs for JavaScript developers. It's an integration with Typescript that may do wonders for you.
d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.
TypeScript is a typed superset of JavaScript that transpiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust components.
You don't need to compile jquery to typescript, you just need to use a definition file that tells Typescript how the JavaScript library works.
Get definitions here: https://github.com/DefinitelyTyped/DefinitelyTyped
or from NuGet if using Visual Studio.
Then just write your typescript as normal, and declare your library if needed:
declare var library : libraryTypedName
for example jquery's d.ts file already does this for you (check the bottom):
declare module "jquery" { export = $; } declare var jQuery: JQueryStatic; declare var $: JQueryStatic;
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jquery
Now in your .ts
file when you type $
it should give you the typescript intellisense.
Now the only things you want to include in your bundleconfig / <script>
are the .js files, both yours and jquery / other libraries. Typescript is COMPILE time only!
The convention in TypeScript is to have a reference.ts
or reference.d.ts
file in your project that will bring in these external references.
So in your reference.ts
file add the path to your jquery.d.ts
(the path will be relative to the reference.ts file):
/// <reference path="../relative/path/to/jquery.d.ts"/>
Then you should be able to use the jQuery definitions in your project.
NOTE: The reference.ts file is a convention but you can actually add a <reference path="..."/>
directive to any TypeScript file if needed.
From the TypeScript Language Specificiation (PDF) 11.1.1:
A comment of the form
/// <reference path="…"/>
adds a dependency on the source file specified in the path argument. The path is resolved relative to the directory of the containing source file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With