23.06.2015 Views

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

TypeScript Language Specification v1.5

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

For purposes of property access and function calls, the apparent members (section 3.10.1) of a union type<br />

U are those that are present in every one of its constituent types, with types that are unions of the<br />

respective apparent members in the constituent types. The following example illustrates the merging of<br />

member types that occurs when union types are created from object types.<br />

interface A {<br />

a: string;<br />

b: number;<br />

}<br />

interface B {<br />

a: number;<br />

b: number;<br />

c: number;<br />

}<br />

var x: A | B;<br />

var a = x.a; // a has type string | number<br />

var b = x.b; // b has type number<br />

var c = x.c; // Error, no property c in union type<br />

Note that 'x.a' has a union type because the type of 'a' is different in 'A' and 'B', whereas 'x.b' simply has<br />

type number because that is the type of 'b' in both 'A' and 'B'. Also note that there is no property 'x.c'<br />

because only 'A' has a property 'c'.<br />

3.4.1 Contextual Union Types<br />

When used as a contextual type (section 4.19), a union type U has those members that are present in any<br />

of its constituent types, with types that are unions of the respective members in the constituent types.<br />

Specifically:<br />

<br />

<br />

<br />

<br />

<br />

Let S be the set of types in U that has a property P. If S is not empty, U has a property P of a union<br />

type of the types of P from each type in S.<br />

Let S be the set of types in U that have call signatures. If S is not empty and the sets of call<br />

signatures of the types in S are identical ignoring return types, U has the same set of call<br />

signatures, but with return types that are unions of the return types of the respective call<br />

signatures from each type in S.<br />

Let S be the set of types in U that have construct signatures. If S is not empty and the sets of<br />

construct signatures of the types in S are identical ignoring return types, U has the same set of<br />

construct signatures, but with return types that are unions of the return types of the respective<br />

construct signatures from each type in S.<br />

Let S be the set of types in U that has a string index signature. If S is not empty, U has a string<br />

index signature of a union type of the types of the string index signatures from each type in S.<br />

Let S be the set of types in U that has a numeric index signature. If S is not empty, U has a<br />

numeric index signature of a union type of the types of the numeric index signatures from each<br />

type in S.<br />

31

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

Saved successfully!

Ooh no, something went wrong!