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.

5 Statements<br />

This chapter describes the static type checking <strong>TypeScript</strong> provides for JavaScript statements. <strong>TypeScript</strong><br />

itself does not introduce any new statement constructs.<br />

5.1 Variable Statements<br />

Variable statements are extended to include optional type annotations and ECMAScript 6 destructuring<br />

declarations.<br />

VariableDeclaration: ( Modified )<br />

SimpleVariableDeclaration<br />

DestructuringVariableDeclaration<br />

A variable declaration is either a simple variable declaration or a destructuring variable declaration.<br />

5.1.1 Simple Variable Declarations<br />

A simple variable declaration introduces a single named variable and optionally assigns it an initial<br />

value.<br />

SimpleVariableDeclaration:<br />

Identifier TypeAnnotation opt Initialiser opt<br />

TypeAnnotation:<br />

: Type<br />

The type T of a variable introduced by a simple variable 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, T is the Any type.<br />

When a variable declaration specifies both a type annotation and an initializer expression, the type of the<br />

initializer expression is required to be assignable to (section 3.10.4) the type given in the type annotation.<br />

Multiple declarations for the same variable name in the same declaration space are permitted, provided<br />

that each declaration associates the same type with the variable.<br />

When a variable declaration has a type annotation, it is an error for that type annotation to use the<br />

typeof operator to reference the variable being declared.<br />

Below are some examples of simple variable declarations and their associated types.<br />

87

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

Saved successfully!

Ooh no, something went wrong!