Importing a JS AMD

Topics: General, Language Specification
Oct 4, 2012 at 3:41 PM

Let's say I have a set of existing pure JS AMD modules that compose a library or framework. Then I write my application in TypeScript, also composed of external modules intended to be compiled to AMD. From within one of my TypeScript modules, how do I import the pre-existing pure JS AMD module?

Developer
Oct 7, 2012 at 4:47 PM

You simply author '.d.ts' files for the JS AMD modules and include them in your compilation using import directives. For example, say you have a JS AMD module 'geometry.js' like the following

define(["exports"], function(exports) {
    exports.makePoint = function(x, y) {
        return { x: x, y: y };
    }
})

You would write the corresponding 'geometry.d.ts' as follows

interface Point {
    x: number;
    y: number;
}
export function makePoint(x: number, y: number): Point;

To import the module you use an import directive. For example, you might write 'test.ts' as follows

import geometry = module("geometry");
var p = geometry.makePoint(10, 20);

To compile you use the '--module amd' flag

tsc --module amd test.ts

 

Oct 7, 2012 at 6:44 PM

Ah. I see. That makes sense. I assume that the source description file should be placed in the same path as the actual AMD module.. and that the compiler would then write the TypeScript amd modules so that they point to the correct "real" module.