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.

3.7.8 Constructor Type Literals<br />

A constructor type literal specifies the type parameters, regular parameters, and return type of a construct<br />

signature.<br />

ConstructorType:<br />

new TypeParameters opt ( ParameterList opt ) => Type<br />

A constructor type literal is shorthand for an object type containing a single construct signature.<br />

Specifically, a constructor type literal of the form<br />

new < T1, T2, ... > ( p1, p2, ... ) => R<br />

is exactly equivalent to the object type literal<br />

{ new < T1, T2, ... > ( p1, p2, ... ) : R }<br />

Note that constructor types with multiple construct signatures cannot be written as constructor type<br />

literals but must instead be written as object type literals.<br />

3.7.9 Type Queries<br />

A type query obtains the type of an expression.<br />

TypeQuery:<br />

typeof TypeQueryExpression<br />

TypeQueryExpression:<br />

Identifier<br />

TypeQueryExpression . IdentifierName<br />

A type query consists of the keyword typeof followed by an expression. The expression is restricted to a<br />

single identifier or a sequence of identifiers separated by periods. The expression is processed as an<br />

identifier expression (section 4.3) or property access expression (section 4.10), the widened type (section<br />

3.11) of which becomes the result. Similar to other static typing constructs, type queries are erased from<br />

the generated JavaScript code and add no run-time overhead.<br />

Type queries are useful for capturing anonymous types that are generated by various constructs such as<br />

object literals, function declarations, and module declarations. For example:<br />

var a = { x: 10, y: 20 };<br />

var b: typeof a;<br />

Above, 'b' is given the same type as 'a', namely { x: number; y: number; }.<br />

40

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

Saved successfully!

Ooh no, something went wrong!