I am trying to work exclusively in typescript for the front end of an ASP.Net MVC application. This is working right now without modules and the dependency tracking is getting onerous. So it is time to bring in RequireJS and use Typescript's support of AMD.
I have read a lot about this and I think I am very close. However, I am running into a problem with the import statement of typescript saying that it can't find the shim modules that are defined in the requireJS configuration. So if typescript would just pass the name of the module through to the require function in the JS code, everything would work great. But it doesn't, it just throws an error. So the question is how am I supposed to write the import statement for these JS shims?
Here is an example: app.ts
import g = require("Api/greeter");
import $ = require("jquery");
import respond = require("respond");
var greeter = new g.Greeter("#content");
greeter.start();
Lets assume this is the main TS for a page and greeter is the view model for that page. But I know that I need jquery and bootstrap on the page. I have this setup for my requireJS config:
///
requirejs.config({
baseUrl: 'Scripts',
paths: {
"jquery": "libs/jquery-3.1.1",
"bootstrap": "libs/bootstrap.min",
"respond" : "libs/respond"
},
shim: {
"bootstrap": { deps: ["jquery"] },
"respond": { deps: ["bootstrap"] }
}
});
require(['App/app']);
Notice I have respond requiring bootstrap and bootstrap requiring jQuery. So having app.ts require respond should (and does) pull in respond, bootstrap and jQuery.
I know I have a redundant require("jQuery") in app.ts. That is for illustrative purposes only. Ie, that line works fine - TS recognizes jQuery as a valid module name and puts it into the resulting compiled JS file. But TS doesn't recognize "respond" as a module name and I get a compile error:
Cannot find module 'respond'.
I have my files arranged like this:
Scripts
->Api
--> Greeter.ts
-> App
--> App.ts
-> libs
--> jQuery-3.1.1.js
--> bootstrap.js
--> respond.js
I thought I was doing well when jQuery worked but how do I get respond or any other pure JS library I have to pull in to work? From what I can tell in the TS docs, the argument to require() is supposed to be a valid path to a JS file because that's how the compiler is going to get the type information. But if that is the case, why does jQuery work the way I have it defined? It is no different than respond.js.
Thanks Greg