23.06.2015 Views

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Unlike a non-instantiated internal module (section 10.1), an external module containing only interface<br />

types and non-instantiated internal modules still has a module instance associated with it, albeit one with<br />

no members.<br />

If an external module contains an export assignment it is an error for the external module to also contain<br />

export declarations. The two types of exports are mutually exclusive.<br />

11.2.4 Export Assignments<br />

An export assignment designates a module member as the entity to be exported in place of the external<br />

module itself.<br />

ExportAssignment:<br />

export = Identifier ;<br />

When an external module containing an export assignment is imported, the local alias introduced by the<br />

external import declaration takes on all meanings of the identifier named in the export assignment.<br />

It is an error for an external module to contain more than one export assignment.<br />

Assume the following example resides in the file 'point.ts':<br />

export = Point;<br />

class Point {<br />

constructor(public x: number, public y: number) { }<br />

static origin = new Point(0, 0);<br />

}<br />

When 'point.ts' is imported in another external module, the import alias references the exported class and<br />

can be used both as a type and as a constructor function:<br />

import Pt = require("./point");<br />

var p1 = new Pt(10, 20);<br />

var p2 = Pt.origin;<br />

Note that there is no requirement that the import alias use the same name as the exported entity.<br />

11.2.5 CommonJS Modules<br />

The CommonJS Modules definition specifies a methodology for writing JavaScript modules with implied<br />

privacy, the ability to import other modules, and the ability to explicitly export members. A CommonJS<br />

compliant system provides a 'require' function that can be used to synchronously load other external<br />

modules to obtain their singleton module instance, as well as an 'exports' variable to which a module can<br />

add properties to define its external API.<br />

148

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

Saved successfully!

Ooh no, something went wrong!