@@ -24504,11 +24504,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
24504
24504
if (signature) {
24505
24505
const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
24506
24506
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);
24512
24516
}
24513
24517
else if (inferredCovariantType) {
24514
24518
inferredType = inferredCovariantType;
0 commit comments