03.10.2012 Views

TypeScript Language Specification

TypeScript Language Specification

TypeScript Language Specification

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.6.1 Type identity<br />

Two types are considered identical when they are both one and the same of the Any, Number, String,<br />

Boolean, Undefined, Null, or Void types, when they are both array types with identical element types, or<br />

when they are both object types with identical sets of members. Member sets of object types are identical<br />

when, one for one,<br />

� properties are identical in name, optionality, and type,<br />

� call signatures are identical in return types and parameter count, kinds, and types,<br />

� construct signatures are identical in return types and parameter count, kinds, and types,<br />

� index signatures are identical in return and parameter types, and<br />

� brands are identical.<br />

Note that these rules imply that structure, not names, of types determine identity. Also, note that<br />

parameter names are not significant when determining identity of signatures.<br />

3.6.2 Subtypes and Supertypes<br />

A type S is a subtype of a type T, and T is a supertype of S, if one of the following is true:<br />

� S and T are identical types.<br />

� T is the Any type.<br />

� S is the Undefined type.<br />

� S is the Null type and T is not the Undefined or Void type.<br />

� S and T are array types with element types SE and TE, and SE is a subtype of TE.<br />

� S and T are object types and, for each member M in T, one of the following is true:<br />

o M is a property and S contains a property of the same name as M and a type that is a<br />

subtype of that of M.<br />

o M is an optional property and S contains no property of the same name as M.<br />

o M is a call, construct or index signature and S contains a call, construct or index signature<br />

N where<br />

� the signatures are of the same kind (call, construct or index),<br />

� the number of non-optional parameters in N is less than or equal to that of M,<br />

� for parameter positions that are present in both signatures, each parameter type<br />

in N is a subtype or supertype of the corresponding parameter type in M,<br />

� the result type of M is Void, or the result type of N is a subtype of that of M.<br />

o M is a brand and S contains the same brand.<br />

When comparing call, construct, or index signatures, parameter names are ignored and rest parameters<br />

correspond to an unbounded expansion of optional parameters of the rest parameter element type.<br />

3.6.3 Assignment Compatibility<br />

Types are required to be assignment compatible in certain circumstances, such as expression and variable<br />

types in assignment statements and argument and parameter types in function calls.<br />

28

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

Saved successfully!

Ooh no, something went wrong!