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.

In a destructuring assignment expression, the type of the expression on the right must be assignable to<br />

the assignment target on the left. An expression of type S is considered assignable to an assignment<br />

target V if one of the following is true:<br />

V is variable and S is assignable to the type of V.<br />

V is an object assignment pattern and, for each assignment property P in V,<br />

o S is the type Any, or<br />

o S has an apparent property with the property name specified in P of a type that is<br />

assignable to the target given in P, or<br />

o P specifies a numeric property name and S has a numeric index signature of a type that is<br />

assignable to the target given in P, or<br />

o S has a string index signature of a type that is assignable to the target given in P.<br />

V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for<br />

each assignment element E in V,<br />

o S is the type Any, or<br />

o S is a tuple-like type (section 3.3.3) with a property named N of a type that is assignable<br />

to the target given in E, where N is the numeric index of E in the array assignment<br />

pattern, or<br />

o S is not a tuple-like type and the numeric index signature type of S is assignable to the<br />

target given in E.<br />

In an assignment property or element that includes a default value, the type of the default value must be<br />

assignable to the target given in the assignment property or element.<br />

When the output target is ECMAScript 6 or higher, destructuring variable assignments remain unchanged<br />

in the emitted JavaScript code.<br />

When the output target is ECMAScript 3 or 5, destructuring variable assignments are rewritten to series of<br />

simple assignments. For example, the destructuring assignment<br />

var x = 1;<br />

var y = 2;<br />

[x, y] = [y, x];<br />

is rewritten to the simple variable assignments<br />

var x = 1;<br />

var y = 2;<br />

_a = [y, x], x = _a[0], y = _a[1];<br />

var _a;<br />

79

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

Saved successfully!

Ooh no, something went wrong!