Skip to content

Commit aba867d

Browse files
authored
Clean up inference of type parameters, contravariant types (#49915)
1 parent 8402d65 commit aba867d

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/compiler/checker.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22562,15 +22562,11 @@ namespace ts {
2256222562
inferFromTypeArguments(getTypeArguments(source as TypeReference), getTypeArguments(target as TypeReference), getVariances((source as TypeReference).target));
2256322563
}
2256422564
else if (source.flags & TypeFlags.Index && target.flags & TypeFlags.Index) {
22565-
contravariant = !contravariant;
22566-
inferFromTypes((source as IndexType).type, (target as IndexType).type);
22567-
contravariant = !contravariant;
22565+
inferFromContravariantTypes((source as IndexType).type, (target as IndexType).type);
2256822566
}
2256922567
else if ((isLiteralType(source) || source.flags & TypeFlags.String) && target.flags & TypeFlags.Index) {
2257022568
const empty = createEmptyObjectTypeFromStringLiteral(source);
22571-
contravariant = !contravariant;
22572-
inferWithPriority(empty, (target as IndexType).type, InferencePriority.LiteralKeyof);
22573-
contravariant = !contravariant;
22569+
inferFromContravariantTypesWithPriority(empty, (target as IndexType).type, InferencePriority.LiteralKeyof);
2257422570
}
2257522571
else if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) {
2257622572
inferFromTypes((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType);
@@ -22583,10 +22579,7 @@ namespace ts {
2258322579
}
2258422580
else if (source.flags & TypeFlags.Substitution) {
2258522581
inferFromTypes((source as SubstitutionType).baseType, target);
22586-
const oldPriority = priority;
22587-
priority |= InferencePriority.SubstituteSource;
22588-
inferFromTypes((source as SubstitutionType).substitute, target); // Make substitute inference at a lower priority
22589-
priority = oldPriority;
22582+
inferWithPriority((source as SubstitutionType).substitute, target, InferencePriority.SubstituteSource); // Make substitute inference at a lower priority
2259022583
}
2259122584
else if (target.flags & TypeFlags.Conditional) {
2259222585
invokeOnce(source, target, inferToConditionalType);
@@ -22637,6 +22630,20 @@ namespace ts {
2263722630
priority = savePriority;
2263822631
}
2263922632

22633+
function inferFromContravariantTypesWithPriority(source: Type, target: Type, newPriority: InferencePriority) {
22634+
const savePriority = priority;
22635+
priority |= newPriority;
22636+
inferFromContravariantTypes(source, target);
22637+
priority = savePriority;
22638+
}
22639+
22640+
function inferToMultipleTypesWithPriority(source: Type, targets: Type[], targetFlags: TypeFlags, newPriority: InferencePriority) {
22641+
const savePriority = priority;
22642+
priority |= newPriority;
22643+
inferToMultipleTypes(source, targets, targetFlags);
22644+
priority = savePriority;
22645+
}
22646+
2264022647
function invokeOnce(source: Type, target: Type, action: (source: Type, target: Type) => void) {
2264122648
const key = source.id + "," + target.id;
2264222649
const status = visited && visited.get(key);
@@ -22700,10 +22707,14 @@ namespace ts {
2270022707
}
2270122708

2270222709
function inferFromContravariantTypes(source: Type, target: Type) {
22710+
contravariant = !contravariant;
22711+
inferFromTypes(source, target);
22712+
contravariant = !contravariant;
22713+
}
22714+
22715+
function inferFromContravariantTypesIfStrictFunctionTypes(source: Type, target: Type) {
2270322716
if (strictFunctionTypes || priority & InferencePriority.AlwaysStrict) {
22704-
contravariant = !contravariant;
22705-
inferFromTypes(source, target);
22706-
contravariant = !contravariant;
22717+
inferFromContravariantTypes(source, target);
2270722718
}
2270822719
else {
2270922720
inferFromTypes(source, target);
@@ -22866,11 +22877,8 @@ namespace ts {
2286622877
inferFromTypes(getFalseTypeFromConditionalType(source as ConditionalType), getFalseTypeFromConditionalType(target));
2286722878
}
2286822879
else {
22869-
const savePriority = priority;
22870-
priority |= contravariant ? InferencePriority.ContravariantConditional : 0;
2287122880
const targetTypes = [getTrueTypeFromConditionalType(target), getFalseTypeFromConditionalType(target)];
22872-
inferToMultipleTypes(source, targetTypes, target.flags);
22873-
priority = savePriority;
22881+
inferToMultipleTypesWithPriority(source, targetTypes, target.flags, contravariant ? InferencePriority.ContravariantConditional : 0);
2287422882
}
2287522883
}
2287622884

@@ -23065,7 +23073,7 @@ namespace ts {
2306523073
const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown;
2306623074
// Once we descend into a bivariant signature we remain bivariant for all nested inferences
2306723075
bivariant = bivariant || kind === SyntaxKind.MethodDeclaration || kind === SyntaxKind.MethodSignature || kind === SyntaxKind.Constructor;
23068-
applyToParameterTypes(source, target, inferFromContravariantTypes);
23076+
applyToParameterTypes(source, target, inferFromContravariantTypesIfStrictFunctionTypes);
2306923077
bivariant = saveBivariant;
2307023078
applyToReturnTypes(source, target, inferFromTypes);
2307123079
}

0 commit comments

Comments
 (0)