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.

3.10.5 Contextual Signature Instantiation<br />

During type argument inference in a function call (section 4.12.2) it is in certain circumstances necessary<br />

to instantiate a generic call signature of an argument expression in the context of a non-generic call<br />

signature of a parameter such that further inferences can be made. A generic call signature A is<br />

instantiated in the context of non-generic call signature B as follows:<br />

<br />

<br />

Using the process described in 3.10.6, inferences for A's type parameters are made from each<br />

parameter type in B to the corresponding parameter type in A for those parameter positions that<br />

are present in both signatures, where rest parameters correspond to an unbounded expansion of<br />

optional parameters of the rest parameter element type.<br />

The inferred type argument for each type parameter is the union type of the set of inferences<br />

made for that type parameter. However, if the union type does not satisfy the constraint of the<br />

type parameter, the inferred type argument is instead the constraint.<br />

3.10.6 Type Inference<br />

In certain contexts, inferences for a given set of type parameters are made from a type S, in which those<br />

type parameters do not occur, to another type T, in which those type parameters do occur. Inferences<br />

consist of a set of candidate type arguments collected for each of the type parameters. The inference<br />

process recursively relates S and T to gather as many inferences as possible:<br />

If T is one of the type parameters for which inferences are being made, S is added to the set of<br />

inferences for that type parameter.<br />

Otherwise, if S and T are references to the same generic type, inferences are made from each type<br />

argument in S to each corresponding type argument in T.<br />

Otherwise, if T is a union type:<br />

o First, inferences are made from S to each constituent type in T that isn't simply one of the<br />

type parameters for which inferences are being made.<br />

o If the first step produced no inferences and exactly one constituent type in T is simply a<br />

type parameter for which inferences are being made, inferences are made from S to that<br />

type parameter.<br />

Otherwise, if S is a union type, inferences are made from each constituent type in S to T.<br />

Otherwise, if S and T are object types, then for each member M in T:<br />

o If M is a property and S contains a property N with the same name as M, inferences are<br />

made from the type of N to the type of M.<br />

o If M is a call signature and a corresponding call signature N exists in S, N is instantiated<br />

with the Any type as an argument for each type parameter (if any) and inferences are<br />

made from parameter types in N to the corresponding parameter types in M for positions<br />

that are present in both signatures, and from the return type of N to the return type of M.<br />

o If M is a construct signature and a corresponding construct signature N exists in S, N is<br />

instantiated with the Any type as an argument for each type parameter (if any) and<br />

inferences are made from parameter types in N to the corresponding parameter types in<br />

54

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

Saved successfully!

Ooh no, something went wrong!