23.06.2015 Views

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

import g = require("./geometry");<br />

var p: g.Point = { x: 10, y: 20 };<br />

the emitted JavaScript would have no dependency on the 'geometry' module and would simply be<br />

var p = { x: 10, y: 20 };<br />

11.2.6 AMD Modules<br />

The Asynchronous Module Definition (AMD) specification extends the CommonJS Modules specification<br />

with a pattern for authoring asynchronously loadable modules with associated dependencies. Using the<br />

AMD pattern, modules are emitted as calls to a global 'define' function taking an array of dependencies,<br />

specified as external module names, and a callback function containing the module body. The global<br />

'define' function is provided by including an AMD compliant loader in the application. The loader arranges<br />

to asynchronously load the module's dependencies and, upon completion, calls the callback function<br />

passing resolved module instances as arguments in the order they were listed in the dependency array.<br />

The "main" and "log" example from above generates the following JavaScript code when compiled for the<br />

AMD pattern.<br />

File main.js:<br />

define(["require", "exports", "./log"], function(require, exports, log) {<br />

log.message("hello");<br />

}<br />

File log.js:<br />

define(["require", "exports"], function(require, exports) {<br />

exports.message = function(s) {<br />

console.log(s);<br />

}<br />

}<br />

The special 'require' and 'exports' dependencies are always present. Additional entries are added to the<br />

dependencies array and the parameter list as required to represent imported external modules. Similar to<br />

the code generation for CommonJS Modules, a dependency entry is generated for a particular imported<br />

module only if the imported module is referenced as a PrimaryExpression somewhere in the body of the<br />

importing module. If an imported module is referenced only as a ModuleName, no dependency is<br />

generated for that module.<br />

150

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!