Addressing CR feedback.
Adding comment to explain source type instantiation. Adding a test case.
This commit is contained in:
parent
b24b28eaaa
commit
d85df9e9fa
|
@ -3675,6 +3675,7 @@ module ts {
|
|||
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper: TypeMapper): Signature {
|
||||
var context = createInferenceContext(signature.typeParameters);
|
||||
forEachMatchingParameterType(contextualSignature, signature, (source, target) => {
|
||||
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
|
||||
inferTypes(context, instantiateType(source, contextualMapper), target);
|
||||
});
|
||||
return getSignatureInstantiation(signature, getInferredTypes(context));
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
//// [contextualSignatureInstantiation3.ts]
|
||||
function map<T, U>(items: T[], f: (x: T) => U): U[]{
|
||||
return items.map(f);
|
||||
}
|
||||
|
||||
function identity<T>(x: T) {
|
||||
return x;
|
||||
}
|
||||
|
||||
function singleton<T>(x: T) {
|
||||
return [x];
|
||||
}
|
||||
|
||||
var xs = [1, 2, 3];
|
||||
|
||||
// Have compiler check that we get the correct types
|
||||
var v1: number[];
|
||||
var v1 = xs.map(identity); // Error if not number[]
|
||||
var v1 = map(xs, identity); // Error if not number[]
|
||||
|
||||
var v2: number[][];
|
||||
var v2 = xs.map(singleton); // Error if not number[][]
|
||||
var v2 = map(xs, singleton); // Error if not number[][]
|
||||
|
||||
|
||||
//// [contextualSignatureInstantiation3.js]
|
||||
function map(items, f) {
|
||||
return items.map(f);
|
||||
}
|
||||
function identity(x) {
|
||||
return x;
|
||||
}
|
||||
function singleton(x) {
|
||||
return [x];
|
||||
}
|
||||
var xs = [1, 2, 3];
|
||||
var v1;
|
||||
var v1 = xs.map(identity);
|
||||
var v1 = map(xs, identity);
|
||||
var v2;
|
||||
var v2 = xs.map(singleton);
|
||||
var v2 = map(xs, singleton);
|
22
tests/cases/compiler/contextualSignatureInstantiation3.ts
Normal file
22
tests/cases/compiler/contextualSignatureInstantiation3.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
function map<T, U>(items: T[], f: (x: T) => U): U[]{
|
||||
return items.map(f);
|
||||
}
|
||||
|
||||
function identity<T>(x: T) {
|
||||
return x;
|
||||
}
|
||||
|
||||
function singleton<T>(x: T) {
|
||||
return [x];
|
||||
}
|
||||
|
||||
var xs = [1, 2, 3];
|
||||
|
||||
// Have compiler check that we get the correct types
|
||||
var v1: number[];
|
||||
var v1 = xs.map(identity); // Error if not number[]
|
||||
var v1 = map(xs, identity); // Error if not number[]
|
||||
|
||||
var v2: number[][];
|
||||
var v2 = xs.map(singleton); // Error if not number[][]
|
||||
var v2 = map(xs, singleton); // Error if not number[][]
|
Loading…
Reference in a new issue