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.

4.18 The Comma Operator<br />

The comma operator permits the operands to be of any type and produces a result that is of the same<br />

type as the second operand.<br />

4.19 Contextually Typed Expressions<br />

Type checking of an expression is improved in several contexts by factoring in the type of the destination<br />

of the value computed by the expression. In such situations, the expression is said to be contextually<br />

typed by the type of the destination. An expression is contextually typed in the following circumstances:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

In a variable, parameter, binding property, binding element, or member declaration, an initializer<br />

expression is contextually typed by<br />

o the type given in the declaration's type annotation, if any, or otherwise<br />

o for a parameter, the contextual type for the declaration (section 4.9.3), if any, or otherwise<br />

o the type implied by the binding pattern in the declaration (section 5.1.3), if any.<br />

In the body of a function declaration, function expression, arrow function, method declaration, or<br />

get accessor declaration that has a return type annotation, return expressions are contextually<br />

typed by the type given in the return type annotation.<br />

In the body of a function expression or arrow function that has no return type annotation, if the<br />

function expression or arrow function is contextually typed by a function type with exactly one call<br />

signature, and if that call signature is non-generic, return expressions are contextually typed by<br />

the return type of that call signature.<br />

In the body of a constructor declaration, return expressions are contextually typed by the<br />

containing class type.<br />

In the body of a get accessor with no return type annotation, if a matching set accessor exists and<br />

that set accessor has a parameter type annotation, return expressions are contextually typed by<br />

the type given in the set accessor's parameter type annotation.<br />

In a typed function call, argument expressions are contextually typed by their corresponding<br />

parameter types.<br />

In a contextually typed object literal, each property value expression is contextually typed by<br />

o the type of the property with a matching name in the contextual type, if any, or otherwise<br />

o for a numerically named property, the numeric index type of the contextual type, if any,<br />

or otherwise<br />

o the string index type of the contextual type, if any.<br />

In a contextually typed array literal expression containing no spread elements, an element<br />

expression at index N is contextually typed by<br />

o the type of the property with the numeric name N in the contextual type, if any, or<br />

otherwise<br />

o the numeric index type of the contextual type, if any.<br />

In a contextually typed array literal expression containing one or more spread elements, an<br />

element expression at index N is contextually typed by the numeric index type of the contextual<br />

type, if any.<br />

80

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

Saved successfully!

Ooh no, something went wrong!