@@ -13752,15 +13752,17 @@ namespace ts {
13752
13752
if (!inferredType) {
13753
13753
const signature = context.signature;
13754
13754
if (signature) {
13755
- if (inference.contraCandidates && (!inference.candidates || inference.candidates.length === 1 && inference.candidates[0].flags & TypeFlags.Never)) {
13756
- // If we have contravariant inferences, but no covariant inferences or a single
13757
- // covariant inference of 'never', we find the best common subtype and treat that
13758
- // as a single covariant candidate.
13759
- inference.candidates = [getContravariantInference(inference)];
13760
- inference.contraCandidates = undefined;
13761
- }
13762
- if (inference.candidates) {
13763
- inferredType = getCovariantInference(inference, signature);
13755
+ const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
13756
+ if (inference.contraCandidates) {
13757
+ const inferredContravariantType = getContravariantInference(inference);
13758
+ // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
13759
+ // unless the co-variant inference is a subtype and not 'never'.
13760
+ inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
13761
+ isTypeSubtypeOf(inferredCovariantType, inferredContravariantType) ?
13762
+ inferredCovariantType : inferredContravariantType;
13763
+ }
13764
+ else if (inferredCovariantType) {
13765
+ inferredType = inferredCovariantType;
13764
13766
}
13765
13767
else if (context.flags & InferenceFlags.NoDefault) {
13766
13768
// We use silentNeverType as the wildcard that signals no inferences.
0 commit comments