I think a lot of the confusion around this is due to jQuery not really acting like an External Module, which inhibits the use of an import
statement. The solution is quite clean, simple and elegant enough to not feel like a work-around.
I have written up a simple example of Using RequireJS and jQuery in TypeScript, which works as follows...
You grab the type definitions from Definitely Typed for RequireJS and jQuery.
You can now use raw RequireJS with static typing inside of the TypeScript file.
app.ts
///<reference path="require.d.ts" />
///<reference path="jquery.d.ts" />
require(['jquery'], function ($) {
$(document).ready(() => {
alert('Your code executes after jQuery has been loaded.');
});
});
And then you only need to add the single script tag to your page:
<script data-main="app" src="require.js"></script>
Benefits over other solutions?
- You can update jQuery and RequireJS independently
- You don't have to rely on shim project being updated
- You don't have to manually load jQuery (or anything else that isn't "like a module" that you have a
.d.ts
file for)