As I mentioned in a previous blog post, one of the great features of TypeScript is the support for definition files. For most popular JavaScript libraries a definition file exists, but last week I wanted to use a jQuery widget that didnāt had a type definition file(yet).
So I decided to create one myself. The important part is to understand how to link your widget to the existing jQuery type definitions.
If you want to extend the $ sign directly, you have to create an interface JQueryStatic and put your functions in it.
If you want to extend the $(āā) selector method, you have to create an interface JQuery and put your functions there.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="../jquery/jquery.d.ts" /> | |
// Type definitions for html2canvas | |
interface JQuery { | |
html2canvas(options?: any): JQuery; | |
} | |
declare var html2canvas: JQueryStatic; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document.body).html2canvas({}); |