Skip to content

Commit 46bd405

Browse files
committed
Better scheme for choosing between co- and contra-variant inferences
1 parent cf2f339 commit 46bd405

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13752,15 +13752,17 @@ namespace ts {
1375213752
if (!inferredType) {
1375313753
const signature = context.signature;
1375413754
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;
1376413766
}
1376513767
else if (context.flags & InferenceFlags.NoDefault) {
1376613768
// We use silentNeverType as the wildcard that signals no inferences.

0 commit comments

Comments
 (0)