@@ -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