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.

A dot notation property access of the form<br />

object . name<br />

where object is an expression and name is an identifier (including, possibly, a reserved word), is used to<br />

access the property with the given name on the given object. A dot notation property access is processed<br />

as follows at compile-time:<br />

<br />

<br />

<br />

If object is of type Any, any name is permitted and the property access is of type Any.<br />

Otherwise, if name denotes an accessible apparent property (section 3.10.1) in the type of object,<br />

the property access is of the type of that property. Public members are always accessible, but<br />

private and protected members of a class have restricted accessibility, as described in 8.2.2.<br />

Otherwise, the property access is invalid and a compile-time error occurs.<br />

A bracket notation property access of the form<br />

object [ index ]<br />

where object and index are expressions, is used to access the property with the name computed by the<br />

index expression on the given object. A bracket notation property access is processed as follows at<br />

compile-time:<br />

If index is a string literal or a numeric literal and object has an apparent property (section 3.10.1)<br />

with the name given by that literal (converted to its string representation in the case of a numeric<br />

literal), the property access is of the type of that property.<br />

Otherwise, if object has an apparent numeric index signature and index is of type Any, the Number<br />

primitive type, or an enum type, the property access is of the type of that index signature.<br />

Otherwise, if object has an apparent string index signature and index is of type Any, the String or<br />

Number primitive type, or an enum type, the property access is of the type of that index<br />

signature.<br />

Otherwise, if index is of type Any, the String or Number primitive type, or an enum type, the<br />

property access is of type Any.<br />

Otherwise, the property access is invalid and a compile-time error occurs.<br />

The rules above mean that properties are strongly typed when accessed using bracket notation with the<br />

literal representation of their name. For example:<br />

var type = {<br />

name: "boolean",<br />

primitive: true<br />

};<br />

var s = type["name"]; // string<br />

var b = type["primitive"]; // boolean<br />

66

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

Saved successfully!

Ooh no, something went wrong!