From 15eaba254b005f08831afa4004515c76812d64ca Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 20 Aug 2018 16:04:11 -0700 Subject: [PATCH] Covariant inferences (other than never) preferred over contravariant --- src/compiler/checker.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 26b93b19335..c9f70463db8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13638,10 +13638,11 @@ namespace ts { if (!inferredType) { const signature = context.signature; if (signature) { - if (inference.contraCandidates) { - // If we have contravariant inferences we find the best common subtype and treat - // that as a single covariant candidate. - inference.candidates = append(inference.candidates, getContravariantInference(inference)); + if (inference.contraCandidates && (!inference.candidates || inference.candidates.length === 1 && inference.candidates[0].flags & TypeFlags.Never)) { + // If we have contravariant inferences, but no covariant inferences or a single + // covariant inference of 'never', we find the best common subtype and treat that + // as a single covariant candidate. + inference.candidates = [getContravariantInference(inference)]; inference.contraCandidates = undefined; } if (inference.candidates) {