Skip to content

Commit 7ae62dc

Browse files
committed
Improve logic that chooses co- vs. contra-variant inferences
1 parent 3c7660a commit 7ae62dc

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24504,11 +24504,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2450424504
if (signature) {
2450524505
const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
2450624506
if (inference.contraCandidates) {
24507-
// If we have both co- and contra-variant inferences, we prefer the contra-variant inference
24508-
// unless the co-variant inference is a subtype of some contra-variant inference and not 'never'.
24509-
inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
24510-
some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ?
24511-
inferredCovariantType : getContravariantInference(inference);
24507+
// If we have both co- and contra-variant inferences, we use the co-variant inference if it is not 'never',
24508+
// it is a subtype of some contra-variant inference, and no other type parameter is constrained to this type
24509+
// parameter and has inferences that would conflict. Otherwise, we use the contra-variant inference.
24510+
const useCovariantType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
24511+
some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) &&
24512+
every(context.inferences, other => other === inference ||
24513+
getConstraintOfTypeParameter(other.typeParameter) !== inference.typeParameter ||
24514+
every(other.candidates, t => isTypeSubtypeOf(t, inferredCovariantType)));
24515+
inferredType = useCovariantType ? inferredCovariantType : getContravariantInference(inference);
2451224516
}
2451324517
else if (inferredCovariantType) {
2451424518
inferredType = inferredCovariantType;

0 commit comments

Comments
 (0)