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.

When the output target is ECMAScript 3 or 5, the function is rewritten to<br />

function drawText(_a) {<br />

var _b = _a.text,<br />

text = _b === void 0 ? "" : _b,<br />

_c = _a.location,<br />

_d = _c === void 0 ? [0, 0] : _c,<br />

x = _d[0],<br />

y = _d[1],<br />

_e = _a.bold,<br />

bold = _e === void 0 ? false : _e;<br />

// Draw text<br />

}<br />

Destructuring parameter declarations do not permit type annotations on the individual binding patterns,<br />

as such annotations would conflict with the already established meaning of colons in object literals. Type<br />

annotations must instead be written on the top-level parameter declaration. For example<br />

interface DrawTextInfo {<br />

text?: string;<br />

location?: [number, number];<br />

bold?: boolean;<br />

}<br />

function drawText({ text, location: [x, y], bold }: DrawTextInfo) {<br />

// Draw text<br />

}<br />

6.5 Generic Functions<br />

A function implementation may include type parameters in its signature (section 3.8.2.1) and is then called<br />

a generic function. Type parameters provide a mechanism for expressing relationships between<br />

parameter and return types in call operations. Type parameters have no run-time representation—they<br />

are purely a compile-time construct.<br />

Type parameters declared in the signature of a function implementation are in scope in the signature and<br />

body of that function implementation.<br />

The following is an example of a generic function:<br />

interface Comparable {<br />

localeCompare(other: any): number;<br />

}<br />

99

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

Saved successfully!

Ooh no, something went wrong!