I have a number of jQuery plugins that I would like to load using the AMD pattern in TypeScript. For example, I might have this structure:
/lib/jquery.myplugin.js
/app.ts
The plugin simply extends jQuery. It provides no new top-level functions or variables. An example might be:
// jquery.myplugin.js
jQuery.fn.myExample = function() { ... }
The corresponding jquery.myplugin.d.ts file looks like:
interface JQuery {
myExample();
}
So that now in app.ts I can call something like $('#my-element').myExample(). Note that this assumes I already have the jquery.d.ts declarations from Microsoft loaded.
My question is how do I load this library asynchronously and take advantage of TypeScripts static typing? I could use it like this:
/// <reference path="lib/jquery.myplugin.d.ts"/>
but that requires me to add a <script> tag to my HTML, and the library is not loaded asynchronously. I want TypeScript to generate this code:
define(["require", "exports", "lib/jquery.myplugin"], function (require, exports, __jquery.myplugin__) {
...
// Use my plugin
$('#my-element').myExample();
}
However since there are no exports in the .d.ts file I can't write import myplugin = module('lib/jquery.myplugin').
The closest I've gotten is to make a jquery.myplugin.d.ts that references another ts file with the interface declaration and includes at least one export. However there is nothing to export in this library, and in order to get the desired output I have to not only add an export but I have to call it.
Update: I have opened a work item for this on typescript.codeplex.com
require([...], () => { ... })in my TypeScript code, which generates a nested require call. Far from ideal, but it works.