Skip to content

Commit 5e55118

Browse files
committed
Only make contravariant inferences from pure contravariant positions
1 parent e36957a commit 5e55118

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
/* @internal */
25
namespace ts {
36
const ambientModuleSymbolRegex = /^".+"$/;
@@ -13460,6 +13463,7 @@ namespace ts {
1346013463
let symbolStack: Symbol[];
1346113464
let visited: Map<boolean>;
1346213465
let contravariant = false;
13466+
let bivariant = false;
1346313467
let propagationType: Type;
1346413468
let allowComplexConstraintInference = true;
1346513469
inferFromTypes(originalSource, originalTarget);
@@ -13546,7 +13550,9 @@ namespace ts {
1354613550
}
1354713551
if (priority === inference.priority) {
1354813552
const candidate = propagationType || source;
13549-
if (contravariant) {
13553+
// We make contravariant inferences only if we are in a pure contravariant position,
13554+
// i.e. only if we have not descended into a bivariant position.
13555+
if (contravariant && !bivariant) {
1355013556
inference.contraCandidates = append(inference.contraCandidates, candidate);
1355113557
}
1355213558
else {
@@ -13798,7 +13804,12 @@ namespace ts {
1379813804

1379913805
function inferFromSignature(source: Signature, target: Signature, skipParameters: boolean) {
1380013806
if (!skipParameters) {
13807+
const saveBivariant = bivariant;
13808+
const kind = target.declaration ? target.declaration.kind : SyntaxKind.Unknown;
13809+
// Once we descend into a bivariant signature we remain bivariant for all nested inferences
13810+
bivariant = bivariant || kind === SyntaxKind.MethodDeclaration || kind === SyntaxKind.MethodSignature || kind === SyntaxKind.Constructor;
1380113811
forEachMatchingParameterType(source, target, inferFromContravariantTypes);
13812+
bivariant = saveBivariant;
1380213813
}
1380313814
const sourceTypePredicate = getTypePredicateOfSignature(source);
1380413815
const targetTypePredicate = getTypePredicateOfSignature(target);

0 commit comments

Comments
 (0)