Skip to content

Instantly share code, notes, and snippets.

@ksummerlin
Last active December 16, 2015 21:59
Show Gist options
  • Select an option

  • Save ksummerlin/5503457 to your computer and use it in GitHub Desktop.

Select an option

Save ksummerlin/5503457 to your computer and use it in GitHub Desktop.
Using third-party libraries in TypeScript with ambient declarations.
/// <reference path="../Scripts/typings/jquery/jquery.d.ts" />
$('a').click(function () {
alert('clicked');
});
@ksummerlin

Copy link
Copy Markdown
Author

Revision #1, attempting to use jQuery will give you a compiler error that '$' is not defined.

@ksummerlin

Copy link
Copy Markdown
Author

Revision #2, we have used the declare keyword to define an ambient declaration. This tells the compiler that an external library will define the variable '$'. Using the 'any' type for the variable will tell the compiler to ignore the standard static typing rules for future '$' references.

@ksummerlin

Copy link
Copy Markdown
Author

In this last revision, we have included a reference to the jQuery declarations file downloaded from Github/DefinitelyTyped. The full declaration file gives you much better intellisense than just using a simple variable declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment