if f is a contextually typed function expression (section 4.9.3), the inferred return type is the union type (section 3.3.4) of the types of the return statement expressions in the function body, ignoring return statements with no expressions.
Otherwise, the inferred return type is the first of the types of the return statement expressions in the function body that is a supertype (section 3.8.3) of each of the others, ignoring return statements with no expressions. A compile-time error occurs if no return statement expression has a type that is a supertype of each of the others.
A union type literal is written as a sequence of types separated by vertical bars.
UnionType:
PrimaryOrUnionType | PrimaryType
A union typle literal references a union type.
When function or constructor types are included in union types they must be enclosed in parentheses
• If each type in U has a string index signature, U has a string index signature of a union type of the types of the string index signatures from each type in U.
• If each type in U has a numeric index signature, U has a numeric index signature of a union type of the types of the numeric index signatures from each type in U.
If each type in U has construct signatures and the sets of construct signatures are identical ignoring return types, U has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in U.
If each type in U has call signatures and the sets of call signatures are identical ignoring return types, U has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in U.
• A union type U is assignable to a type T if each type in U is assignable to T.
• A type T is assignable to a union type U if T is assignable to any type in U.
A union type encompasses an unordered set of unrelated types (that is, types that aren’t subtypes of each other). The following rules govern union types:
• A | B is equivalent to A if B is a subtype of A.
• A | B is equivalent to B | A.
• AB | C is equivalent to A | BC, where AB is A | B and BC is B | C.