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.

Otherwise, no type is associated with the binding property and an error occurs.<br />

The type T associated with a binding element is determined as follows:<br />

Let S be the type associated with the immediately containing destructuring variable declaration,<br />

binding property, or binding element.<br />

If S is the Any type:<br />

o If the binding element specifies an initializer expression, T is the type of that initializer<br />

expression.<br />

o Otherwise, T is the Any type.<br />

If S is not an array-like type (section 3.3.2), no type is associated with the binding property and an<br />

error occurs.<br />

If the binding element is a rest element, T is an array type with an element type E, where E is the<br />

type of the numeric index signature of S.<br />

Otherwise, if S is a tuple-like type (section 3.3.3):<br />

o Let N be the zero-based index of the binding element in the array binding pattern.<br />

o If S has a property with the numerical name N, T is the type of that property.<br />

o Otherwise, no type is associated with the binding element and an error occurs.<br />

Otherwise, if S has a numeric index signature, T is the type of the numeric index signature.<br />

Otherwise, no type is associated with the binding element and an error occurs.<br />

When a destructuring variable declaration, binding property, or binding element specifies an initializer<br />

expression, the type of the initializer expression is required to be assignable to the widened form of the<br />

type associated with the destructuring variable declaration, binding property, or binding element.<br />

When the output target is ECMAScript 6 or higher, except for removing the optional type annotation,<br />

destructuring variable declarations remain unchanged in the emitted JavaScript code.<br />

When the output target is ECMAScript 3 or 5, destructuring variable declarations are rewritten to simple<br />

variable declarations. For example, an object destructuring declaration of the form<br />

var { x, p: y, q: z = false } = getSomeObject();<br />

is rewritten to the simple variable declarations<br />

var _a = getSomeObject(),<br />

x = _a.x,<br />

y = _a.p,<br />

_b = _a.q,<br />

z = _b === void 0 ? false : _b;<br />

The '_a' and '_b' temporary variables exist to ensure the assigned expression is evaluated only once, and<br />

the expression 'void 0' simply denotes the JavaScript value 'undefined'.<br />

90

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

Saved successfully!

Ooh no, something went wrong!