This project is read-only.

How do I import an AMD module that's only resolvable at runtime? (using RequireJS.config with path+shim)

Topics: General
Apr 22, 2013 at 5:14 PM
Edited Apr 22, 2013 at 5:30 PM
I have a project using RequireJS and AMD modules. Now I want to import some old-skool javascript modules. Since I use RequireJS I followed this guide to setup require.config with paths and shims:

http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/

But this guide only has the modules accessible in the main file using a require(), while I want to import() these modules in a TypeScript submodule (one that also has some other import/export statements).
import myShimmed = module('myShimName');
import myRegular = module('myRegularModule');

export class MyClass {
    //etc
}
The compiler won't do this because it cannot find the 'myShimName' module I've setup in the RequireJS config. The only way I can make it load the dependency runtime without compiler error is to use an AMD define(), but then I cannot define classes and modules.

So how do I let TypeScript generate the proper require statements without complaining about missing files?

edit: I know I could load the shimmed modules in the regular require in the main file and use some globals but that defeats the whole purpose of dependency resolution.
Apr 23, 2013 at 1:59 AM
If I understand correctly, you can use declare modules to help fill out the information of what is being imported for the TypeScript compiler. Something like:
declare module "myModule" {
    export function moduleFunction(): string;
}

import baz = module("myModule");