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.5 Type Parameters<br />

A type parameter represents an actual type that the parameter is bound to in a generic type reference or<br />

a generic function call. Type parameters have constraints that establish upper bounds for their actual type<br />

arguments.<br />

Since a type parameter represents a multitude of different type arguments, type parameters have certain<br />

restrictions compared to other types. In particular, a type parameter cannot be used as a base class or<br />

interface.<br />

3.5.1 Type Parameter Lists<br />

Class, interface, and function declarations may optionally include lists of type parameters enclosed in <<br />

and > brackets. Type parameters are also permitted in call signatures of object, function, and constructor<br />

type literals.<br />

TypeParameters:<br />

< TypeParameterList ><br />

TypeParameterList:<br />

TypeParameter<br />

TypeParameterList , TypeParameter<br />

TypeParameter:<br />

Identifier Constraint opt<br />

Constraint:<br />

extends Type<br />

Type parameter names must be unique. A compile-time error occurs if two or more type parameters in<br />

the same TypeParameterList have the same name.<br />

The scope of a type parameter extends over the entire declaration with which the type parameter list is<br />

associated, with the exception of static member declarations in classes.<br />

Each type parameter has an associated type parameter constraint that establishes an upper bound for<br />

type arguments. Omitting a constraint corresponds to specifying the empty object type {}. Type<br />

parameters declared in a particular type parameter list may not be referenced in constraints in that type<br />

parameter list.<br />

The base constraint of a type parameter T is defined as follows:<br />

If T has no declared constraint, T's base constraint is the empty object type {}.<br />

If T's declared constraint is a type parameter, T's base constraint is that of the type parameter.<br />

Otherwise, T's base constraint is T's declared constraint.<br />

In the example<br />

32

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

Saved successfully!

Ooh no, something went wrong!