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.

T > ( ... ) => { ... }<br />

could be parsed as an arrow function expression with a type parameter or a type assertion applied to an<br />

arrow function with no type parameter. It is resolved as the former, but parentheses can be used to select<br />

the latter meaning:<br />

< T > ( ( ... ) => { ... } )<br />

4.9.3 Contextually Typed Function Expressions<br />

When a function expression with no type parameters and no parameter type annotations is contextually<br />

typed (section 4.19) by a type T and a contextual signature S can be extracted from T, the function<br />

expression is processed as if it had explicitly specified parameter type annotations as they exist in S.<br />

Parameters are matched by position and need not have matching names. If the function expression has<br />

fewer parameters than S, the additional parameters in S are ignored. If the function expression has more<br />

parameters than S, the additional parameters are all considered to have type Any.<br />

Likewise, when a function expression with no return type annotation is contextually typed (section 4.19) by<br />

a function type T and a contextual signature S can be extracted from T, expressions in contained return<br />

statements (section 5.7) are contextually typed by the return type of S.<br />

A contextual signature S is extracted from a function type T as follows:<br />

<br />

<br />

<br />

If T is a function type with exactly one call signature, and if that call signature is non-generic, S is<br />

that signature.<br />

If T is a union type, let U be the set of element types in T that have call signatures. If each type in<br />

U has exactly one call signature and that call signature is non-generic, and if all of the signatures<br />

are identical ignoring return types, then S is a signature with the same parameters and a union of<br />

the return types.<br />

Otherwise, no contextual signature can be extracted from T and S is undefined.<br />

In the example<br />

var f: (s: string) => string = s => s.toLowerCase();<br />

the function expression is contextually typed by the type of 'f', and since the function expression has no<br />

type parameters or type annotations its parameter type information is extracted from the contextual type,<br />

thus inferring the type of 's' to be the String primitive type.<br />

4.10 Property Access<br />

A property access uses either dot notation or bracket notation. A property access expression is always<br />

classified as a reference.<br />

65

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

Saved successfully!

Ooh no, something went wrong!