Skip to content

Commit 7a31706

Browse files
committed
Only check constraints after type argument inference is complete
1 parent ae6c9cd commit 7a31706

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14788,7 +14788,7 @@ namespace ts {
1478814788
return getWidenedType(unwidenedType);
1478914789
}
1479014790

14791-
function getInferredType(context: InferenceContext, index: number): Type {
14791+
function getInferredType(context: InferenceContext, index: number, ignoreConstraints = false): Type {
1479214792
const inference = context.inferences[index];
1479314793
let inferredType = inference.inferredType;
1479414794
if (!inferredType) {
@@ -14836,11 +14836,13 @@ namespace ts {
1483614836

1483714837
inference.inferredType = inferredType;
1483814838

14839-
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
14840-
if (constraint) {
14841-
const instantiatedConstraint = instantiateType(constraint, context);
14842-
if (!context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
14843-
inference.inferredType = inferredType = instantiatedConstraint;
14839+
if (!ignoreConstraints) {
14840+
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
14841+
if (constraint) {
14842+
const instantiatedConstraint = instantiateType(constraint, context);
14843+
if (!context.compareTypes(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {
14844+
inference.inferredType = inferredType = instantiatedConstraint;
14845+
}
1484414846
}
1484514847
}
1484614848
}
@@ -14852,10 +14854,10 @@ namespace ts {
1485214854
return isInJavaScriptFile ? anyType : emptyObjectType;
1485314855
}
1485414856

14855-
function getInferredTypes(context: InferenceContext): Type[] {
14857+
function getInferredTypes(context: InferenceContext, ignoreConstraints = false): Type[] {
1485614858
const result: Type[] = [];
1485714859
for (let i = 0; i < context.inferences.length; i++) {
14858-
result.push(getInferredType(context, i));
14860+
result.push(getInferredType(context, i, ignoreConstraints));
1485914861
}
1486014862
return result;
1486114863
}
@@ -19925,7 +19927,10 @@ namespace ts {
1992519927
inferTypes(context.inferences, spreadType, restType);
1992619928
}
1992719929

19928-
return getInferredTypes(context);
19930+
// If we are excluding context sensitive arguments we know there is another round of type inference
19931+
// coming, so we omit constraint checking. Otherwise, when a type parameter's constraint is self-
19932+
// referential, checking the constraint would cause inferences to be fixed prematurely.
19933+
return getInferredTypes(context, !!excludeArgument);
1992919934
}
1993019935

1993119936
function getArrayifiedType(type: Type) {

0 commit comments

Comments
 (0)