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 the example<br />

var x = 1;<br />

function f(a = x) {<br />

var x = "hello";<br />

}<br />

the local variable 'x' is in scope in the parameter initializer (thus hiding the outer 'x'), but it is an error to<br />

reference it because it will always be uninitialized at the time the parameter initializer is evaluated.<br />

6.4 Destructuring Parameter Declarations<br />

Parameter declarations can specify binding patterns (section 3.8.2.2) and are then called destructuring<br />

parameter declarations. Similar to a destructuring variable declaration (section 5.1.2), a destructuring<br />

parameter declaration introduces zero or more named locals and initializes them with values extracted<br />

from properties or elements of the object or array passed as an argument for the parameter.<br />

The type of local introduced in a destructuring parameter declaration is determined in the same manner<br />

as a local introduced by a destructuring variable declaration, except the type T associated with a<br />

destructuring parameter declaration is determined as follows:<br />

If the declaration includes a type annotation, T is that type.<br />

Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.11)<br />

of the type of the initializer expression.<br />

Otherwise, if the declaration specifies a binding pattern, T is the implied type of that binding<br />

pattern (section 5.1.3).<br />

Otherwise, if the parameter is a rest parameter, T is any[].<br />

Otherwise, T is any.<br />

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

destructuring parameter declarations remain unchanged in the emitted JavaScript code. When the output<br />

target is ECMAScript 3 or 5, destructuring parameter declarations are rewritten to local variable<br />

declarations.<br />

The example<br />

function drawText({ text = "", location: [x, y] = [0, 0], bold = false }) {<br />

// Draw text<br />

}<br />

declares a function drawText that takes a single parameter of the type<br />

{ text?: string; location?: [number, number]; bold?: boolean; }<br />

98

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

Saved successfully!

Ooh no, something went wrong!