@@ -5715,7 +5715,7 @@ namespace ts {
5715
5715
function getInferenceMapper(context: InferenceContext): TypeMapper {
5716
5716
if (!context.mapper) {
5717
5717
const mapper: TypeMapper = t => {
5718
- const typeParameters = context.typeParameters;
5718
+ const typeParameters = context.signature. typeParameters;
5719
5719
for (let i = 0; i < typeParameters.length; i++) {
5720
5720
if (t === typeParameters[i]) {
5721
5721
context.inferences[i].isFixed = true;
@@ -5724,7 +5724,7 @@ namespace ts {
5724
5724
}
5725
5725
return t;
5726
5726
};
5727
- mapper.mappedTypes = context.typeParameters;
5727
+ mapper.mappedTypes = context.signature. typeParameters;
5728
5728
mapper.context = context;
5729
5729
context.mapper = mapper;
5730
5730
}
@@ -7508,15 +7508,12 @@ namespace ts {
7508
7508
}
7509
7509
7510
7510
function createInferenceContext(signature: Signature, inferUnionTypes: boolean): InferenceContext {
7511
- const typeParameters = signature.typeParameters;
7512
- const returnType = getReturnTypeOfSignature(signature);
7513
7511
const inferences = map(signature.typeParameters, createTypeInferencesObject);
7514
7512
return {
7515
- typeParameters,
7516
- returnType,
7513
+ signature,
7517
7514
inferUnionTypes,
7518
7515
inferences,
7519
- inferredTypes: new Array(typeParameters.length),
7516
+ inferredTypes: new Array(signature. typeParameters.length),
7520
7517
};
7521
7518
}
7522
7519
@@ -7547,6 +7544,7 @@ namespace ts {
7547
7544
}
7548
7545
7549
7546
function inferTypes(context: InferenceContext, source: Type, target: Type) {
7547
+ const typeParameters = context.signature.typeParameters;
7550
7548
let sourceStack: Type[];
7551
7549
let targetStack: Type[];
7552
7550
let depth = 0;
@@ -7606,7 +7604,6 @@ namespace ts {
7606
7604
if (source.flags & TypeFlags.ContainsAnyFunctionType) {
7607
7605
return;
7608
7606
}
7609
- const typeParameters = context.typeParameters;
7610
7607
for (let i = 0; i < typeParameters.length; i++) {
7611
7608
if (target === typeParameters[i]) {
7612
7609
const inferences = context.inferences[i];
@@ -7646,7 +7643,7 @@ namespace ts {
7646
7643
let typeParameter: TypeParameter;
7647
7644
// First infer to each type in union or intersection that isn't a type parameter
7648
7645
for (const t of targetTypes) {
7649
- if (t.flags & TypeFlags.TypeParameter && contains(context. typeParameters, t)) {
7646
+ if (t.flags & TypeFlags.TypeParameter && contains(typeParameters, t)) {
7650
7647
typeParameter = <TypeParameter>t;
7651
7648
typeParameterCount++;
7652
7649
}
@@ -7805,10 +7802,11 @@ namespace ts {
7805
7802
// we made at least one inference that wasn't shallow, or
7806
7803
// the type parameter has a primitive type constraint, or
7807
7804
// the type parameter wasn't fixed and is referenced at top level in the return type.
7808
- const keepLiteralTypes = !context.inferences[index].shallow ||
7809
- hasPrimitiveConstraint(context.typeParameters[index]) ||
7810
- !context.inferences[index].isFixed && hasTypeParameterAtTopLevel(context.returnType, context.typeParameters[index]);
7811
- const baseInferences = keepLiteralTypes ? inferences : map(inferences, getBaseTypeOfLiteralType);
7805
+ const signature = context.signature;
7806
+ const widenLiteralTypes = context.inferences[index].shallow &&
7807
+ !hasPrimitiveConstraint(signature.typeParameters[index]) &&
7808
+ (context.inferences[index].isFixed || !hasTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index]));
7809
+ const baseInferences = widenLiteralTypes ? map(inferences, getBaseTypeOfLiteralType) : inferences;
7812
7810
// Infer widened union or supertype, or the unknown type for no common supertype
7813
7811
const unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences);
7814
7812
inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType;
@@ -7826,7 +7824,7 @@ namespace ts {
7826
7824
7827
7825
// Only do the constraint check if inference succeeded (to prevent cascading errors)
7828
7826
if (inferenceSucceeded) {
7829
- const constraint = getConstraintOfTypeParameter(context.typeParameters[index]);
7827
+ const constraint = getConstraintOfTypeParameter(context.signature. typeParameters[index]);
7830
7828
if (constraint) {
7831
7829
const instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context));
7832
7830
if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
0 commit comments