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.

9.4 Constant Enum Declarations<br />

An enum declaration that specifies a const modifier is a constant enum declaration. In a constant enum<br />

declaration, all members must have constant values and it is an error for a member declaration to specify<br />

an expression that isn't classified as a constant enum expression.<br />

Unlike regular enum declarations, constant enum declarations are completely erased in the emitted<br />

JavaScript code. For this reason, it is an error to reference a constant enum object in any other context<br />

than a property access that selects one of the enum's members. For example:<br />

const enum Comparison {<br />

LessThan = -1,<br />

EqualTo = 0,<br />

GreaterThan = 1<br />

}<br />

var x = Comparison.EqualTo; // Ok, replaced with 0 in emitted code<br />

var y = Comparison[Comparison.EqualTo]; // Error<br />

var z = Comparison; // Error<br />

The entire const enum declaration is erased in the emitted JavaScript code. Thus, the only permitted<br />

references to the enum object are those that are replaced with an enum member value.<br />

9.5 Code Generation<br />

An enum declaration generates JavaScript equivalent to the following:<br />

var ;<br />

(function () {<br />

<br />

})(||(={}));<br />

EnumName is the name of the enum.<br />

EnumMemberAssignments is a sequence of assignments, one for each enum member, in order they are<br />

declared, of the form<br />

[[""] = ] = "";<br />

where MemberName is the name of the enum member and Value is the assigned constant value or the<br />

code generated for the computed value expression.<br />

For example, the 'Color' enum example from section 9.1 generates the following JavaScript:<br />

132

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

Saved successfully!

Ooh no, something went wrong!